Getting “number of referencing and referenced columns for foreign key disagree” in PostgreSql

狂风中的少年 提交于 2021-01-04 02:43:17

问题


I'm creating a few tables in pgAdmin 4, but I keep getting the error it the title for some reason, can you spot why? I don't see any problem and i've looked into other code samples similar to mine which compiled perfectely. It also runs OK in IDEone (http://ideone.com/ZBn2Nr).

thanks!

Create table item
    (iname varchar(30) primary key,
    itype varchar(30));

Create table Cafe
    (license numeric(5,0) primary key,
    cname varchar(30),
    address varchar(30));

Create table Client
    (cid numeric(5,0) primary key,
    name varchar(30),
    phone numeric(9,0));

Create table Likes
    (cid numeric(5,0),
    iname varchar(30),
    primary key(cid,iname),
    foreign key(cid) references Client,
    foreign key(iname) references item);

Create table Sells
    (license numeric(5,0),
    iname varchar(30),
    price float check(price > 0),
    primary key(license,iname),
    foreign key(license) references Cafe,
    foreign key(iname) references item);

Create table Receipt
    (cid numeric(5,0),
    rno numeric(5,0),
    license numeric(5,0),
    rdate date,
    primary key(cid,rno),
    foreign key(cid) references Client,
    foreign key(license) references Cafe);

Create table Buys
    (cid numeric(5,0),
    rno numeric(5,0),
    iname varchar(30),
    amount int check(amount > 0),
    primary key(cid,rno,iname),
    foreign key(cid) references Client,
    foreign key(rno) references Receipt,
    foreign key(iname) references item);

回答1:


When you do not specify a column list in your references clause it will expand to the primary key of the referenced table.

For example:

foreign key(rno) references Receipt

expands to

foreign key(rno) references Receipt(cid,rno)

so the number of columns doesn't match



来源:https://stackoverflow.com/questions/45346269/getting-number-of-referencing-and-referenced-columns-for-foreign-key-disagree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!