【问题标题】:Unable to add some foreign key fields无法添加一些外键字段
【发布时间】:2017-12-29 00:12:44
【问题描述】:

自从我升级到 Django 2.0 后,我在运行添加外键字段的迁移时遇到了一个奇怪的错误

django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table "auth_user"

有什么想法吗?

这是我的模型类:

class Tag(models.Model):
    """
    """
    titles = models.ManyToManyField('main.Sentence')
    parent = models.ForeignKey('Tag',null=True,blank=True, on_delete=models.SET_NULL)
    author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    created = models.DateTimeField(auto_now_add=True)

这是它的迁移文件实例化:

    migrations.CreateModel(
        name='Tag',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('created', models.DateTimeField(auto_now_add=True)),
            ('author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
            ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='coach.Tag')),
            ('titles', models.ManyToManyField(to='main.Sentence')),
        ],
        options={
            'ordering': ['-created'],
        },
    ),

以下是 SQL 的生成方式:

ALTER TABLE "coach_tag" ADD CONSTRAINT "coach_tag_author_id_6e9296b3_fk_auth_user_id" FOREIGN KEY ("author_id") REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED;

SQL Error [42830]: ERROR: there is no unique constraint matching given keys for referenced table "auth_user"
  org.postgresql.util.PSQLException: ERROR: there is no unique constraint matching given keys for referenced table "auth_user"

我确实重命名了我的数据库中的一些表,并且必须通过 SQL 进行一些修改才能使事情正常进行。我正在使用 PostgreSQL。

  • 我重命名了表格
  • 我确定我重命名了 django_content_type 中的条目
  • 我没有重命名 Postgres“序列”中的条目,但每个表的 auto-inc 主键似乎仍然指向旧的命名序列,所以我认为这不是问题

我需要以某种方式手动添加外键约束吗?

auth_user 表

CREATE TABLE public.auth_user ( 
    id int4 NOT NULL DEFAULT nextval('auth_user_id_seq'::regclass), 
    password varchar(128) NOT NULL, 
    last_login timestamptz NULL, 
    is_superuser bool NOT NULL, 
    username varchar(150) NOT NULL, 
    first_name varchar(30) NOT NULL, 
    last_name varchar(150) NOT NULL, 
    email varchar(254) NOT NULL, 
    is_staff bool NOT NULL, 
    is_active bool NOT NULL, 
    date_joined timestamptz NOT NULL 
) WITH ( OIDS=FALSE ) ;

【问题讨论】:

  • auth_user.id 上是否没有唯一(或主键)约束?这就是抱怨,因为父表需要有引用的属性是唯一的。显示该表的 DDL,例如 \d auth_user 如果您使用的是 psql 客户端。
  • 它只是标准的 django 模型。虽然它是在我升级到 2.0 之前使用 1.10 版本构建的,但不确定这是否重要。 CREATE TABLE public.auth_user ( id int4 NOT NULL DEFAULT nextval('auth_user_id_seq'::regclass), password varchar(128) NOT NULL, last_login timestamptz NULL, is_superuser bool NOT NULL, username varchar(150) NOT NULL, first_name varchar(30 ) NOT NULL, last_name varchar(150) NOT NULL, email varchar(254) NOT NULL, is_staff bool NOT NULL, is_active bool NOT NULL, date_joined timestamptz NOT NULL) WITH (OIDS=FALSE);
  • id 列缺少 UNIQUE/PK 约束,如消息所述。我对 Django 了解不多,但我很惊讶 FK 以前会使用这个 DDL。尝试添加 PK alter table auth_user add constraint auth_user_id_pk primary key (id);
  • 是的!这样就解决了问题。我不知道我的 django 在第一次构建表时没有添加这些约束,但至少我有一个修复。泰!

标签: django postgresql


【解决方案1】:

运行alter table auth_user add constraint auth_user_id_pk primary key (id);解决了这个问题。 当我将一些模型移动到另一个应用程序并手动编辑迁移文件时,这发生在我身上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2017-05-10
    • 1970-01-01
    • 2012-11-08
    • 2020-09-26
    相关资源
    最近更新 更多