我使用inspectdb
工具通過內省現有數據庫來創建模型。Django中的多對多關係「ProgrammingError:列shoe_size_run.id不存在」
部分ERD:
class Shoe(models.Model):
sku = models.AutoField(primary_key=True)
style_number = models.CharField(unique=True, max_length=6)
factory_style_number = models.CharField(unique=True, max_length=10, blank=True, null=True)
stock_name = models.CharField(max_length=10)
season = models.CharField(max_length=30)
date_created = models.DateField()
category_code = models.ForeignKey(Category, models.DO_NOTHING, db_column='category_code')
brand_code = models.ForeignKey(Brand, models.DO_NOTHING, db_column='brand_code')
factory_number = models.ForeignKey(Factory, models.DO_NOTHING, db_column='factory_number')
spec_sheet = models.ForeignKey('SpecSheet', models.DO_NOTHING, blank=True, null=True)
class Meta:
managed = False
db_table = 'shoe'
class ShoeSizeRun(models.Model):
sku = models.ForeignKey(Shoe, models.DO_NOTHING, db_column='sku')
size_run = models.ForeignKey('SizeRun', models.DO_NOTHING)
class Meta:
managed = False
db_table = 'shoe_size_run'
unique_together = (('sku', 'size_run'),)
class SizeRun(models.Model):
size_run_id = models.AutoField(primary_key=True)
run = models.IntegerField()
width = models.CharField(max_length=4)
pairs_of_five = models.IntegerField(blank=True, null=True)
pairs_of_five_and_half = models.IntegerField(blank=True, null=True)
pairs_of_six = models.IntegerField(blank=True, null=True)
pairs_of_six_and_half = models.IntegerField(blank=True, null=True)
pairs_of_seven = models.IntegerField(blank=True, null=True)
pairs_of_seven_and_half = models.IntegerField(blank=True, null=True)
pairs_of_eight = models.IntegerField(blank=True, null=True)
pairs_of_eight_and_half = models.IntegerField(blank=True, null=True)
pairs_of_nine = models.IntegerField(blank=True, null=True)
pairs_of_nine_and_half = models.IntegerField(blank=True, null=True)
pairs_of_ten = models.IntegerField(blank=True, null=True)
pairs_of_ten_and_half = models.IntegerField(blank=True, null=True)
pairs_of_eleven = models.IntegerField(blank=True, null=True)
pairs_of_eleven_and_half = models.IntegerField(blank=True, null=True)
pairs_of_twelve = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'size_run'`
雖然,這些都是爲那些表的SQL定義:使用多對一
CREATE TABLE shoe (
sku SERIAL NOT NULL,
style_number varchar(6) NOT NULL UNIQUE,
factory_style_number varchar(10) UNIQUE,
stock_name varchar(10) NOT NULL,
season varchar(30) NOT NULL,
date_created date NOT NULL,
category_code int4 NOT NULL,
brand_code int4 NOT NULL,
factory_number int4 NOT NULL,
spec_sheet_id int4,
PRIMARY KEY (sku));
CREATE TABLE shoe_size_run (
sku int4 NOT NULL,
size_run_id int4 NOT NULL,
PRIMARY KEY (sku,
size_run_id));
CREATE TABLE size_run (
size_run_id SERIAL NOT NULL,
run int4 NOT NULL,
width varchar(4) NOT NULL,
pairs_of_five int4,
pairs_of_five_and_half int4,
pairs_of_six int4,
pairs_of_six_and_half int4,
pairs_of_seven int4,
pairs_of_seven_and_half int4,
pairs_of_eight int4,
pairs_of_eight_and_half int4,
pairs_of_nine int4,
pairs_of_nine_and_half int4,
pairs_of_ten int4,
pairs_of_ten_and_half int4,
pairs_of_eleven int4,
pairs_of_eleven_and_half int4,
pairs_of_twelve int4,
PRIMARY KEY (size_run_id));
ALTER TABLE shoe_size_run ADD CONSTRAINT FKshoe_size_544815 FOREIGN KEY (sku) REFERENCES shoe (sku);
ALTER TABLE shoe_size_run ADD CONSTRAINT FKshoe_size_544404 FOREIGN KEY (size_run_id) REFERENCES size_run (size_run_id);
現在,我有問題在Django中使用多對多的表。爲什麼這個錯誤?
我該如何解決這個問題?
請複製堆棧跟蹤的文本內容(「切換到複製和粘貼視圖」),並將其替換爲截圖。同時發佈錯誤發生時正在調用的視圖代碼。 – solarissmoke
@solarissmoke我剛加了,謝謝。 – Fabio