Strange foreign key behavior on empty tables in SQLite 3

后端 未结 1 489
北荒
北荒 2021-01-25 19:27

I have SQLite 3 with the following setup (simplified):

create table Location(LocationId integer not null,
                      LocationCode text not null, 
             


        
相关标签:
1条回答
  • 2021-01-25 19:49

    The documentation (hidden in the source code) says:

    A foreign key constraint requires that the key columns in the parent table are collectively subject to a UNIQUE or PRIMARY KEY constraint. […] If the required index cannot be found, either because:

    1. The named parent key columns do not exist, or
    2. The named parent key columns do exist, but are not subject to a UNIQUE or PRIMARY KEY constraint, or
    3. No parent key columns were provided explicitly as part of the foreign key definition, and the parent table does not have a PRIMARY KEY, or
    4. No parent key columns were provided explicitly as part of the foreign key definition, and the PRIMARY KEY of the parent table consists of a a different number of columns to the child key in the child table.

    then … a "foreign key mismatch" error [is raised].

    > DELETE FROM Child;
    Error: foreign key mismatch
    > CREATE UNIQUE INDEX di ON Department(DepartmentId);
    > DELETE FROM Child;
    > 
    
    0 讨论(0)
提交回复
热议问题