Circular dependency in Django ForeignKey?

后端 未结 2 634
醉酒成梦
醉酒成梦 2021-01-22 05:34

I have two models in Django:

A:
  b = ForeignKey(\"B\")

B:
  a = ForeignKey(A)

I want these ForeignKeys to be non-NULL.

However, I can

2条回答
  •  轮回少年
    2021-01-22 05:58

    If this is really a bootstrapping problem and not something that will reoccur during normal usage, you could just create a fixture that will prepopulate your database with some initial data. The fixture-handling code includes workarounds at the database layer to resolve the forward-reference issue.

    If it's not a bootstrapping problem, and you're going to want to regularly create these circular relations among new objects, you should probably either reconsider your schema--one of the foreign keys is probably unnecessary.

提交回复
热议问题