Why cannot delete from a table with reference to another

戏子无情 提交于 2019-12-20 07:09:09

问题


I have looked for answers but maybe I miss something. I have 2 tables see below. The entities from the first table is referenced from the second one, but when I try to delete from the second I get Error: foreign key mismatch. There are triggers (not show here), but they have nothing to do with DELETE or cmdauth. I do not understand why cannot remove row?

CREATE TABLE app (name TEXT, script TEXT, PRIMARY KEY(name));
CREATE TABLE env (name TEXT, PRIMARY KEY(name));
CREATE TABLE role (name TEXT, command TEXT, PRIMARY KEY(name,command));

CREATE TABLE cmdauth (groupname TEXT, rolename TEXT, appname TEXT, envname TEXT, FOREIGN KEY (appname) REFERENCES app(name), FOREIGN KEY (rolename) REFERENCES role(name), FOREIGN KEY (envname) REFERENCES env(name), PRIMARY KEY (groupname,rolename,appname,envname));

sqlite> select * from cmdauth where appname='app1' and groupname='admin' and envname='test' and rolename='restarter';
admin|restarter|app1|test
sqlite> delete from cmdauth where appname='app1' and groupname='admin' and envname='test' and rolename='restarter';
Error: foreign key mismatch

Thank you for your help!


回答1:


The documentation says:

Usually, the parent key of a foreign key constraint is the primary key of the parent table. If they are not the primary key, then the parent key columns must be collectively subject to a UNIQUE constraint or have a UNIQUE index.

This requirement is not met for the reference to role(name).



来源:https://stackoverflow.com/questions/21380623/why-cannot-delete-from-a-table-with-reference-to-another

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