The problem is that when you did a.type = a
, a did not yet exist in the database, therefore it has no pk.
One way around this is to save twice, first to save it into the database, referring to a dummy object that's already in the database, then save it again once you can get it from the database. One problem with this is there's sort of a chicken and egg problem of actually getting such an object into the database in the first place. I'd deal with this by means of creating a fixture to run each time you use sync-db, so that one such object exists and is valid.
Another option is to relax the not-null constraint and be diligent about getting them assigned.
Either way, inserting into such a table will always be a pain because you can't get the PK to use without first inserting, and you can't insert without having a key to use in the reference field, regardless of ORM or database or anything.
an alternative solution is to break the circular dependency and do something like:
AliasType(django.db.Model):
pass
Alias(MyBaseModel):
type = models.ForeignKey(AliasType)
a = Alias()
a_t = AliasType()
a_t.save()
a.type = a_t
a.save()
You can still get useful information by using django's very smart accessors,
AliasType.objects.all()[0].alias_set
which can get you back to the original Alias object without having an explicit link to it.