I'm using django 1.6, and I have 2 models referencing each other like so:
class Person(models.Model):
address = models.ForeignKey('Address', blank=False)
class Address(models.Model):
person = models.ForeignKey(Person, blank=False)
The reason I have cyclic foreign key's is for data integrity. Because an address may be assigned to exactly one user, and a user must have at least one address. And yes, I know I can live without this, but I would rather not.
I'm using PostgreSQL with my foreign keys DEFERRABLE INITIALLY DEFERRED
, and I want to use transactions to insert the data into those tables.
However, when I try to use transaction.atomic():
, it does not behave as expected within my view.
Here is what I am doing:
with transaction.atomic():
addr = Address(street='125 fake street')
addr.save()
person = Person()
person.address_id = addr.id
addr.person_id = person.id
addr.save()
person.save()
However, I keep getting an IntegrityError
the first time I call addr.save()
Any suggestions what I'm doing wrong? Is there a better way to to that?