Comportamento estranho de chave estrangeira em tabelas vazias no SQLite 3
Eu tenho o SQLite 3 com a seguinte configuração (simplificada):
create table Location(LocationId integer not null,
LocationCode text not null,
primary key(LocationId),
unique(LocationCode));
A tabela acima está sendo referenciada pelo Departamento:
create table Department(DepartmentId integer not null,
LocationId integer not null,
DepartmentCode text not null,
primary key(LocationId, DepartmentCode),
foreign key(LocationId) references Location(LocationId));
A tabela acima está sendo referenciada por Child:
create table Event(EventId integer not null,
LocationId integer not null,
unique(LocationId, EventDate),
primary key(eventId),
foreign key(LocationId) references Location(LocationId));
A tabela acima faz referência à localização da tabela:
create table Parent(ParentId integer not null,
EmailAddress text not null,
primary key(ParentId),
unique(EmailAddress));
A tabela acima está sendo referenciada pela tabela Child:
create table Child(ChildId integer not null,
ParentId integer not null,
ChildCode text not null,
DepartmentId integer not null,
primary key(ChildId, ParentId),
foreign key(ParentId) references Parent(ParentId),
foreign key(DepartmentId) references Department(DepartmentId));
O filho da tabela é o que eu estou tentando excluir.
Neste ponto, o banco de dados inteiro está vazio e tem "pragma foreign_keys = ON".
Ao testar um script para limpar o banco de dados, encontrei um erro ao excluir da tabela vaziaChild
ter uma chave estrangeira para a tabela (também vazia)Parent
.
Quando eu emito o comandodelete from child
(enquanto já está vazio), o SQLite3 volta com a mensagem de erro "mismatch de chaves estrangeiras".
Esta é a parte vital do script de exclusão:
delete from Child;
delete from Parent;
delete from Event;
delete from Department;
delete from Location;
Eu vi alguns posts aqui sobre a desativação temporária do suporte de chave estrangeira, mas isso não faz sentido para mim. Isso faz com que todo o processo de implementação de uma relação de chave estrangeira seja desnecessário.