我将我的数据库从MySql迁移到PostgreSQL,并且,由于我注意到许多表格没有更新,一些谷歌搜索告诉我这样做:
ALTER SEQUENCE my_table_id_seq OWNED BY my_table.id;
事情开始奏效了。我必须为我的所有桌子做这件事。
然后我意识到在创建了一个django.contrib.auth.model.User并尝试使用post_save
钩子创建一个UserProfile后,我会得到一个IntegrityError
,因为{{1外键发送为null。
这里发送到PostgreSQL的user_id
和query
(在Python中):
args
我想发生的事情是,INSERT INTO "auth_user" ("username", "first_name", "last_name", "email", "password", "is_staff", "is_active", "is_superuser", "last_login", "date_joined") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) (u'apitest04', '', '', u'a@a.aa', 'sha1$9f796$69c88c635c8a53a91544765d7996747cf326cfec', False, True, False, u'2013-01-05 12:39:07.384198', u'2013-01-05 12:39:07.384198')
INSERT INTO "myapp_userprofile" ("user_id", "website", "job", "hobbies", "timezone", "about", "company_name", "company_description", "company_website", "retailer_country", "avatar", "default_license", "default_watermark_text", "default_watermark", "default_watermark_position", "default_watermark_opacity", "language") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) (None, None, None, None, None, None, None, None, None, None, None, 0, None, False, 0, 10, None)
的序列ID在事务关闭之前没有提前。我怀疑因为auth_user
的创建在User
的创建失败时被回滚,所以它们必须在同一事务中发生。
UserProfile
的{{1}}是否有可能尚未返回,因为交易尚未结束?您如何建议我解决问题?
答案 0 :(得分:1)
问题已经解决,而且发生了,因为auth_user.id
序列没有所有者。这修好了它:
ALTER SEQUENCE auth_auth_id_seq OWNED BY auth_auth.id;