Django中的多对多关系" ProgrammingError:column shoe_size_run.id不存在"

时间:2016-05-12 10:27:45

标签: django database postgresql

我使用inspectdb工具通过内省现有数据库来创建模型。

部分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中使用多对多表时遇到问题。为什么会出现这个错误?

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您的表格shoe_size_run有一个复合主键(sku, size_run_id)。不幸的是,Django还不支持这个(见ticket 373

如果要在Django中使用该表,则必须向该表添加单个列主键。最简单的列名是id,因为这是Django默认的预期。

查看图表,您还需要为shoe_image表执行相同的操作。

答案 1 :(得分:0)

表格为shoe_size_run,列为size_run_id。因此,您的模型字段必须为size_run_id而不是size_run