2017-09-08 256 views
6

我有一個django/postgresql應用程序。每當我跑我的最新的遷移,我收到以下錯誤:Django遷移失敗

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', 

    ), 

] 

這是這個模型我現在的表configuaration:

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

+0

您是否將某些值默認定義爲專用值? – Marlysson

+0

你可以顯示你的'models.py'的代碼嗎? – williezh

回答

4

看起來你正在運行到this記載Django的錯誤。該錯誤是triaged as invalid(以及正確的),所以不幸的是沒有乾淨的解決方案。

在數據庫中定義的唯一一起約束如圖

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

如果你想刪除此約束,你需要確保在遷移文件中的unique_together定義與數據庫的定義一致。嘗試用此代替AlterUniqueTogether行:

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