sqlite3 “foreign key constraint failed”

后端 未结 2 1599
[愿得一人]
[愿得一人] 2021-01-03 23:25

I\'ve set up two tables:

CREATE TABLE A
(
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    name TEXT
 );

CREATE TABLE B
(
    id INTEGER NOT NULL PRIM         


        
相关标签:
2条回答
  • 2021-01-04 00:03

    The "problem" is that you have set a foreign key on table B.

    foreign key(id2) references A(id)
    

    This means that column id2 in table B references column id in table A. Both Lord of the Rings and Catch 22 from Table B are linked to John from Table A. Therefore you cannot delete John without first deleting these other two entries from Table B first.

    The alternative would be to remove the foreign key.

    See this documentation for more details.

    0 讨论(0)
  • 2021-01-04 00:09

    Table B has rows whose foreign key references the primary key value of the Table A row you are trying to delete so deleting it would violate the integrity of your database.

    You could include ON DELETE CASCADE in your foreign key definition. With that, when you delete an entry from Table A, any entries in Table B linked to the deleted row would also be deleted. Don't know if that's appropriate for your application.

    0 讨论(0)
提交回复
热议问题