首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取Django迁移错误“序列必须具有与其链接的表相同的所有者”,但所有表和序列都具有相同的所有者

获取Django迁移错误“序列必须具有与其链接的表相同的所有者”,但所有表和序列都具有相同的所有者
EN

Stack Overflow用户
提问于 2022-01-18 19:57:44
回答 1查看 776关注 0票数 2

我试图在Django项目中运行迁移。(Django 3.1,Python3.9.9)我在我的虚拟环境中。我一直有个令人费解的错误。

代码语言:javascript
复制
 python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, intake, sessions
Running migrations:
  Applying intake.0021_auto_20220115_1147...Traceback (most recent call last):
  File "/Users/me/Sites/client/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.ObjectNotInPrerequisiteState: sequence must have same owner as table it is linked to

但是当我列出我的表和序列时,它们都有相同的所有者。

代码语言:javascript
复制
intake=# \dt
                    List of relations
 Schema |            Name            | Type  |   Owner   
--------+----------------------------+-------+-----------
 public | auth_group                 | table | dbuser
 public | auth_group_permissions     | table | dbuser
 public | auth_permission            | table | dbuser
 public | auth_user                  | table | dbuser
 public | auth_user_groups           | table | dbuser
 public | auth_user_user_permissions | table | dbuser
 public | django_admin_log           | table | dbuser
 public | django_content_type        | table | dbuser
 public | django_migrations          | table | dbuser
 public | django_session             | table | dbuser
 public | intake_byattorney          | table | dbuser
 public | intake_client              | table | dbuser
(12 rows)

intake=# \ds
                         List of relations
 Schema |               Name                |   Type   |   Owner   
--------+-----------------------------------+----------+-----------
 public | auth_group_id_seq                 | sequence | dbuser
 public | auth_group_permissions_id_seq     | sequence | dbuser
 public | auth_permission_id_seq            | sequence | dbuser
 public | auth_user_groups_id_seq           | sequence | dbuser
 public | auth_user_id_seq                  | sequence | dbuser
 public | auth_user_user_permissions_id_seq | sequence | dbuser
 public | django_admin_log_id_seq           | sequence | dbuser
 public | django_content_type_id_seq        | sequence | dbuser
 public | django_migrations_id_seq          | sequence | dbuser
 public | intake_byattorney_id_seq          | sequence | dbuser
 public | intake_client_id_seq              | sequence | dbuser
(11 rows)

为什么会这样?我是否需要将表和序列的所有权更改为其他东西,如postgres

这是在本地和我的生产机器上发生的。

以下是所涉迁移的内容:

代码语言:javascript
复制
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('intake', '0020_alter_client_status'),
    ]

    operations = [
        migrations.AlterField(
            model_name='byattorney',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
        migrations.AlterField(
            model_name='client',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
        migrations.AlterField(
            model_name='client',
            name='status',
            field=models.CharField(choices=[('1__intake', 'Stage 1: Needs initial contact'), ('2__attorney-review', 'Stage 2: Needs attorney contact'), ('3__evaluation', 'Stage 3: Evaluation - Accept/Reject'), ('4__final-disposition', 'Stage 4: Final Disposition'), ('5__client-accepted', 'Client Accepted'), ('6__client-rejected', 'Client Rejected')], max_length=20),
        ),
    ]

下面是‘pythonsqlMigrationAdsion0021’的输出:

代码语言:javascript
复制
python manage.py sqlmigrate intake 0021
BEGIN;
--
-- Alter field id on byattorney
--
SET CONSTRAINTS "intake_client_by_attorney_id_680583a2_fk" IMMEDIATE; ALTER TABLE "intake_client" DROP CONSTRAINT "intake_client_by_attorney_id_680583a2_fk";
ALTER TABLE "intake_byattorney" ALTER COLUMN "id" TYPE integer USING "id"::integer;
DROP SEQUENCE IF EXISTS "intake_byattorney_id_seq" CASCADE;
CREATE SEQUENCE "intake_byattorney_id_seq";
ALTER TABLE "intake_byattorney" ALTER COLUMN "id" SET DEFAULT nextval('"intake_byattorney_id_seq"');
SELECT setval('"intake_byattorney_id_seq"', MAX("id")) FROM "intake_byattorney";
ALTER SEQUENCE "intake_byattorney_id_seq" OWNED BY "intake_byattorney"."id";
ALTER TABLE "intake_client" ALTER COLUMN "by_attorney_id" TYPE integer USING "by_attorney_id"::integer;
ALTER TABLE "intake_client" ADD CONSTRAINT "intake_client_by_attorney_id_680583a2_fk" FOREIGN KEY ("by_attorney_id") REFERENCES "intake_byattorney" ("id") DEFERRABLE INITIALLY DEFERRED;
--
-- Alter field id on client
--
ALTER TABLE "intake_client" ALTER COLUMN "id" TYPE integer USING "id"::integer;
DROP SEQUENCE IF EXISTS "intake_client_id_seq" CASCADE;
CREATE SEQUENCE "intake_client_id_seq";
ALTER TABLE "intake_client" ALTER COLUMN "id" SET DEFAULT nextval('"intake_client_id_seq"');
SELECT setval('"intake_client_id_seq"', MAX("id")) FROM "intake_client";
ALTER SEQUENCE "intake_client_id_seq" OWNED BY "intake_client"."id";
--
-- Alter field status on client
--
COMMIT;
EN

回答 1

Stack Overflow用户

发布于 2022-03-09 11:24:38

在id上makemigrations之后,我的项目与BigAutoField完全一样的问题。事实证明,此SQL错误是由前一个问题引起的。如果您运行了一个./manage.py showmigrations,它应该提到您的0021摄入量迁移没有完成。

代码语言:javascript
复制
intake
 [X] 0001_initial
 [X] 0002_auto_20220214_1030
 [X] ...
 [ ] intake.0021_auto_20220115_1147

但是,您的数据库中的django_migrations表应该列出这个intake.0021迁移。这意味着它已经被应用了。

代码语言:javascript
复制
SELECT name FROM django_migrations WHERE app = 'intake' AND starts_with(name, '0021');

但是,列出的DB名称可能与现有文件名0021_auto_20220115_1147略有不同(run @ 11:47)。例如DB:0021_auto_20220115_1056 (run @ 10:56)。这可能是由于在与当前使用的(dev)服务器不同的服务器上执行的makemigrations

根据DB中django_migrations中的内容更改文件名,然后再次运行manage.py migrate,它应该可以工作。

此错误是由ALTER语句引起的。

代码语言:javascript
复制
ALTER SEQUENCE "intake_byattorney_id_seq" OWNED BY "intake_byattorney"."id";
ALTER SEQUENCE "intake_client_id_seq" OWNED BY "intake_client"."id";

使用postgres,AutoField主键由序列管理。为什么django不使用ALTER SEQUENCE seq_name as bigint语法而不是DROP创建?我不知道,但是postgres dbuser所有者由manage.py完成,这是问题的根源,因为与该序列相关联的table.column具有dbuser所有者

因此,愿意将新创建的序列与适当的table.column与不同的所有者关联起来的table.column语句的执行将触发sequence must have same owner as table it is linked to错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70761679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档