为什么我会收到 "there is no unique constraint matching given keys for referenced table..." 错误

Why am I getting "there is no unique constraint matching given keys for referenced table..." Error

根据此 SO Answer没有匹配给定键的唯一约束引用 table“...” 是您在以下情况下遇到的错误您对不唯一的列进行外键引用调用。但为什么我会在这里收到此错误?

postgres=# create database example;
CREATE DATABASE

postgres=# \c example
You are now connected to database "masterdb" as user "postgres".

masterdb=# create table foo(
id bigserial
);
CREATE TABLE

masterdb=# create table bar (
id bigserial,
foo_fid bigint not null references foo (id)
);
ERROR:  there is no unique constraint matching given keys for referenced table "foo"

您的 table 必须在引用的列上具有唯一索引:

CREATE TABLE foo(
    id BIGSERIAL UNIQUE
);