我很难把外键应用到postgres的音乐数据库中。
我用了这些很好的指令让我走到了这一步:
https://bitbucket.org/lalinsky/mbslave
我把所有的数据都加载到postgres了。
我创建了主键和索引。
然而,当我试图应用外键时,我会得到一个奇怪的错误:
musicbrainz@ip-10-217-5-18:/home/ubuntu/mbslave$ psql < sql/CreateFKConstraints.sql
ERROR: insert or update on table "annotation" violates foreign key constraint "annotation_fk_editor"
DETAIL: Key (editor)=(51298) is not present in table "editor".它似乎在SQL脚本的第一行上出错:
ALTER TABLE annotation
ADD CONSTRAINT annotation_fk_editor
FOREIGN KEY (editor)
REFERENCES editor(id);我刚接触过postgres,很难理解这个错误信息。
第一行使我相信已经创建了外键约束。但我不认为它有。
musicbrainz=> SELECT
musicbrainz-> tc.constraint_name, tc.table_name, kcu.column_name,
musicbrainz-> ccu.table_name AS foreign_table_name,
musicbrainz-> ccu.column_name AS foreign_column_name
musicbrainz-> FROM
musicbrainz-> information_schema.table_constraints AS tc
musicbrainz-> JOIN information_schema.key_column_usage AS kcu
musicbrainz-> ON tc.constraint_name = kcu.constraint_name
musicbrainz-> JOIN information_schema.constraint_column_usage AS ccu
musicbrainz-> ON ccu.constraint_name = tc.constraint_name
musicbrainz-> WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name='annotation';
constraint_name | table_name | column_name | foreign_table_name | foreign_column_name
-----------------+------------+-------------+--------------------+---------------------
(0 rows)错误消息中的第二行令人困惑。听起来好像在编辑器表中找不到id列。但它就在那里。
musicbrainz=> \d editor
Table "musicbrainz.editor"
Column | Type | Modifiers
---------------------+--------------------------+-----------------------------------------------------
id | integer | not null default nextval('editor_id_seq'::regclass)
name | character varying(64) | not null
privs | integer | default 0
email | character varying(64) | default NULL::character varying
website | character varying(255) | default NULL::character varying
bio | text |
member_since | timestamp with time zone | default now()
email_confirm_date | timestamp with time zone |
last_login_date | timestamp with time zone | default now()
edits_accepted | integer | default 0
edits_rejected | integer | default 0
auto_edits_accepted | integer | default 0
edits_failed | integer | default 0
last_updated | timestamp with time zone | default now()
birth_date | date |
gender | integer |
area | integer |
password | character varying(128) | not null
ha1 | character(32) | not null
deleted | boolean | not null default false
Indexes:
"editor_pkey" PRIMARY KEY, btree (id)
"editor_idx_name" UNIQUE, btree (lower(name::text))有什么建议吗?
发布于 2014-08-29 01:00:46
制约因素:
ALTER TABLE annotation
ADD CONSTRAINT annotation_fk_editor
FOREIGN KEY (editor)
REFERENCES editor(id);这意味着在表annotation中,该表中的列editor也必须存在于列id下的表editor中。
从本质上说,这是为了确保所有在editor中使用的id都存在于数据库中。
这有助于维护引用的完整性。没有Foreign约束,任何东西都可以存在,不管它是否实际在editor表中,这可能导致大量与数据相关的问题。
错误信息:
错误:在表上插入或更新“注释”违反外键约束"annotation_fk_editor“详细信息: key (编辑器)=(51298)不在表”编辑器“中。
意味着id of 51298的行不存在于表editor中。
那张表里有数据吗?
您可以在annotations上签入FK约束,方法是在该表上执行一个\d,就像对editor那样。(您可能需要做\d+来获取所有信息。)
此外,虽然我以前没有使用过MusicBrainz,但BitBucket链接包含了两件突出的东西,它们可能会导致您的问题:
./mbslave-import.py mbdump.tar.bz2 mbdump-derived.tar.bz2 (显然没有一个完整的数据库转储,或者至少没有一个显而易见的转储)。https://stackoverflow.com/questions/25560126
复制相似问题