# 出版社 class Publisher(models.Model): # 自增、主鍵 id id = models.AutoField(primary_key=True) # varchar(32)、不爲空、惟一索引 name = models.CharField(max_length=32, null=False, unique=True) # 書 class Booker(models.Model): id = models.AutoField(primary_key=True) title = models.CharField(max_length=64, null=False, unique=True) publisher = models.ForeignKey(to="Publisher")
報錯spa
解決方案code
publisher = models.ForeignKey(to="Publisher", on_delete=models.CASCADE)
問題blog
解決索引
publisher = models.ForeignKey(to="Publisher", on_delete=models.CASCADE, default=1)