[英]Multiple ForeignKey to the same field in Django
我不能向同一個字段添加兩個外鍵約束?
我對這種關系不是 100% 確定,所以我已經包括了這些表格。 希望它也可以幫助其他人學習 Django: Transaction
+----------+-----+---------------------+
| guid | plu | datetime |
+----------+-----+---------------------+
| 00003516 | 1 | 2015-09-22 12:11:12 |
| 0000386a | 2 | 2015-02-22 12:11:10 |
| 0000c59d | 2 | 2015-03-22 12:11:10 |
| 0000f03f | 3 | 2015-01-22 12:12:12 |
+----------+-----+---------------------+
普拉
+-----+------+
| plu | name |
+-----+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
+-----+------+
金融
+-----+------------+------------+-------+
| plu | from | to | price |
+-----+------------+------------+-------+
| 1 | 2013-01-22 | 2015-01-23 | 10 |
| 1 | 2015-01-23 | 2015-02-29 | 20 |
| 2 | 2013-01-22 | 2015-01-22 | 10 |
| 3 | 2013-01-22 | 2015-01-22 | 30 |
+-----+------------+------------+-------+
基於此,我創建了以下模型:財務
class Finance(models.Model):
guid = models.AutoField(primary_key=True)
from = models.DateField()
to = models.DateField()
price = models.DecimalField(max_digits=10, decimal_places=2)
邏輯單元
class Plu(models.Model):
plu = models.IntegerField(primary_key=True)
name = models.CharField(max_length=200, null=True)
交易
class Transaction(models.Model):
guid = models.CharField(primary_key=True, max_length=38)
plu = models.ForeignKey(Plu, db_column='plu')
finance = models.ForeignKey(Finance, db_column='plu')
datetime = models.DateTimeField()
當我嘗試運行它時,我收到以下錯誤 - 該怎么辦?:
app.Transaction: (models.E007) Field 'finance' has column name 'plu' that is used by another field.
HINT: Specify a 'db_column' for the field.
您已復制並粘貼並包含相同的 db_column 名稱,您需要更改它
finance = models.ForeignKey(Finance, db_column='finance')
注意:您實際上根本不需要指定 db_column,因為您只是將其設置為字段的名稱
finance = models.ForeignKey(Finance)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.