[英]PostgreSQL: table referencing only part of a composite primary key
I am Creating a movie and tv show streaming service database(for school project) using Postgres 9.6.2. 我正在使用Postgres 9.6.2创建一个电影和电视节目流媒体服务数据库(用于学校项目)。 I am running into the following error:
我遇到以下错误:
there is no unique constraint matching given keys for referenced table "watchedepisodes" 对于引用表“watchedepisodes”的给定键,没有唯一约束匹配
The TVratings table below will take a tv show, as long as a user has watched at least one episode (that shows up in the WatchedEpisodes table) and allow the user to rate it. 只要用户观看了至少一集(显示在WatchedEpisodes表中)并允许用户对其进行评级,下面的TVratings表将进行电视节目。 Since WatchedEpisodes has a composite primary key of the user id, tv show id, season, and episode, it won't let me just reference from that table a composite key of just uid and tid.
由于WatchedEpisodes具有用户ID,电视节目ID,季节和剧集的复合主键,因此我不会仅仅从该表中引用uid和tid的复合键。
CREATE TABLE WatchedEpisodes (
uid int unique references Users(uid),
tid int,
season int,
episode int,
FOREIGN KEY(tid, season, episode) REFERENCES Episodes(tid, season, episode),
PRIMARY KEY (uid, tid, season, episode)
);
CREATE TABLE TVRatings (
uid int,
tid int,
rating int check (rating > 0 and rating < 6),
FOREIGN KEY(uid, tid) REFERENCES WatchedEpisodes(uid,tid),
PRIMARY KEY(uid, tid)
);
Wondering if there is a way to only reference part of that composite key. 想知道是否有办法只引用该复合键的一部分。 These are only two of my tables so if further information is needed, I can add more.
这些只是我的两个表,所以如果需要进一步的信息,我可以添加更多。
This is actually in the spec, but it's not implemented yet. 这实际上是在规范中,但它还没有实现。 It's called
MATCH PARTIAL
它叫做
MATCH PARTIAL
CREATE TABLE foo (
a int,
b int,
c int,
PRIMARY KEY (a,b,c)
);
CREATE TABLE bar (
a int,
b int,
FOREIGN KEY (a,b) REFERENCES foo (a,b) MATCH PARTIAL
);
You can read about it in the docs , 你可以在文档中阅读它 ,
A value inserted into the referencing column(s) is matched against the values of the referenced table and referenced columns using the given match type.
插入到引用列中的值将使用给定的匹配类型与引用的表和引用的列的值进行匹配。 There are three match types: MATCH FULL, MATCH PARTIAL, and MATCH SIMPLE (which is the default).
有三种匹配类型:MATCH FULL,MATCH PARTIAL和MATCH SIMPLE(默认值)。 MATCH FULL will not allow one column of a multicolumn foreign key to be null unless all foreign key columns are null;
除非所有外键列都为空,否则MATCH FULL将不允许多列外键的一列为空; if they are all null, the row is not required to have a match in the referenced table.
如果它们都为null,则该行不需要在引用的表中具有匹配项。 MATCH SIMPLE allows any of the foreign key columns to be null;
MATCH SIMPLE允许任何外键列为空; if any of them are null, the row is not required to have a match in the referenced table.
如果它们中的任何一个为null,则该行不需要在引用的表中具有匹配项。 MATCH PARTIAL is not yet implemented.
MATCH PARTIAL尚未实施。 (Of course, NOT NULL constraints can be applied to the referencing column(s) to prevent these cases from arising.)
(当然,NOT NULL约束可以应用于引用列,以防止出现这些情况。)
I think currently it's viewed as an anti-feature, so it's not likely to land anytime soon. 我认为目前它被视为一种反功能,所以不太可能很快降落。
As a workaround, you can create another table that just has (a,b)
作为一种解决方法,您可以创建另一个只有
(a,b)
CREATE TABLE baz (
a int,
b int,
PRIMARY KEY (a,b)
);
From here you can link both tables to it... 从这里你可以将两个表链接到它...
CREATE TABLE foo (
a int,
b int,
c int,
PRIMARY KEY (a,b,c),
FOREIGN KEY (a,b) REFERENCES baz
);
CREATE TABLE bar (
a int,
b int,
FOREIGN KEY (a,b) REFERENCES baz
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.