2016-05-12 38 views
0

我使用inspectdb工具通過內省現有數據庫來創建模型。Django中的多對多關係「ProgrammingError:列shoe_size_run.id不存在」

部分ERD:

ERD models.py:

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中使用多對多的表。爲什麼這個錯誤?

我該如何解決這個問題?

+0

請複製堆棧跟蹤的文本內容(「切換到複製和粘貼視圖」),並將其替換爲截圖。同時發佈錯誤發生時正在調用的視圖代碼。 – solarissmoke

+0

@solarissmoke我剛加了,謝謝。 – Fabio

回答

1

您的表shoe_size_run有一個複合主鍵(sku, size_run_id)。不幸的是,Django目前還不支持(見ticket 373

如果你想使用Django中的表,你必須添加一個單一列主鍵到該表。最簡單的列名是id,因爲這是Django默認預期的。

看着你的圖表,你也需要爲shoe_image表做同樣的事情。

+0

所以,我應該添加一個名爲'id'的代理鍵,保存'sku'和'size_run_id'就像外鍵(但不是主鍵)。對? – Fabio

+0

因爲我應該編輯現有的數據庫,是唯一可能的解決方案嗎? – Fabio

+0

我不知道任何其他解決方案。 – Alasdair

0

該表爲shoe_size_run,列爲size_run_id。所以你的模型領域需要是size_run_id而不是size_run