gpt4 book ai didi

python - Django 迁移失败

转载 作者:太空狗 更新时间:2023-10-29 20:19:41 25 4
gpt4 key购买 nike

我有一个 django/postgresql 应用程序。每当我运行最新的迁移时,我都会收到以下错误:

ValueError: Found wrong number (0) of constraints for package(speciality, title)

我认为我需要自定义迁移,但我应该进行哪些更改。

这是迁移:

operations = [

    migrations.AddField(

model_name='package',

name='speciality',

field=models.ManyToManyField(related_name='specialities', to='speciality.Speciality', blank=True),

),

migrations.AlterField(

model_name='package',

name='title',

field=models.CharField(unique=True, max_length=50),

),

migrations.AlterUniqueTogether(

name='package',

unique_together=set([]),

),

migrations.RemoveField(

model_name='package',

name='speciality',

),

]

这是我当前对该模型的表格配置:

sleepyfish=# \d package Table "public.package" Column | Type | Modifiers
---------------+--------------------------+------------------------------------------------------ id | integer | not null default nextval('package_id_seq'::regclass) created_at | timestamp with time zone | not null updated_at | timestamp with time zone | not null title | character varying(50) | not null description | text | status | boolean
| not null price | numeric(8,2) | not null speciality_id | integer | Indexes: "package_pkey" PRIMARY KEY, btree (id) "package_speciality_id_3aeb5c97679442e4_uniq" UNIQUE CONSTRAINT, btree (speciality_id, title) "package_66db61fe" btree (speciality_id) Foreign-key constraints: "package_speciality_id_4255b58fe1ae00c0_fk_speciality_id" FOREIGN KEY (speciality_id) REFERENCES speciality(id) DEFERRABLE INITIALLY DEFERRED Referenced by: TABLE "claimedpackage" CONSTRAINT "claimedpackage_package_id_9e1da358fbb9a46_fk_package_id" FOREIGN KEY (package_id) REFERENCES package(id) DEFERRABLE INITIALLY DEFERRED TABLE "package_service" CONSTRAINT "package_service_package_id_3b0ea08bfcd8da76_fk_package_id" FOREIGN KEY (package_id) REFERENCES package(id) DEFERRABLE INITIALLY DEFERRED

最佳答案

看起来您遇到了 this记录的 Django 错误。错误是 triaged as invalid (这是正确的),所以不幸的是,这个问题没有干净的解决方案。

数据库定义中的unique together约束显示为

"package_speciality_id_3aeb5c97679442e4_uniq" UNIQUE CONSTRAINT,
btree (speciality_id, title)

如果要移除此约束,则需要确保迁移文件中的unique_together 定义与数据库定义一致。尝试将 AlterUniqueTogether 行替换为:

migrations.AlterUniqueTogether(
name='package',
unique_together=set([('speciality_id', 'title')]),
),

关于python - Django 迁移失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46119879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com