SQLITE problem when creating a table with two Foreign Key

1

I'm doing a project on Android with a table structure.

But it turns out that the db does not create it correctly and I can not see what happens.

It is about 4 tables with their relations between them, it lets me create the three first tables but in the table nights I get an error.

CREATE TABLE Usuarios (Id_Usuario INTEGER PRIMARY KEY AUTOINCREMENT, Nombre 
TEXT, Apellidos TEXT, DNI TEXT);

CREATE TABLE Dietas (Id_Dieta INTEGER PRIMARY KEY AUTOINCREMENT, 
Id_Usuario_FK INTEGER, FOREIGN KEY(Id_Usuario_FK) REFERENCES 
Usuarios(Id_Usuario));

CREATE TABLE Proyectos(Id_Proyecto INTEGER PRIMARY KEY AUTOINCREMENT, Nombre 
TEXT, Analitica TEXT);

CREATE TABLE Noches (Id_Noche INTEGER PRIMARY KEY AUTOINCREMENT, Cantidad 
INTEGER, Id_Dieta_FK INTEGER, FOREIGN KEY(Id_Dieta_FK) REFERENCES 
Dietas(Id_Dieta), Id_Proyecto_FK INTEGER, FOREIGN KEY(Id_Proyecto_FK) 
REFERENCES Proyectos(Id_Proyecto));

I have executed it on the browser of SQLite and if I create the table Nights with a single Foreign key , any of them, yes that I create it, but if I put both at the same time no, I understand that the problem comes out there ...

Someone can give me some light.

Thanks !!

    
asked by Raúl Cuc 16.10.2018 в 12:22
source

1 answer

2

You have to put the FOREIGN KEY after the definition of the fields, not interspersed with them:

CREATE TABLE Noches (
    Id_Noche INTEGER PRIMARY KEY AUTOINCREMENT, 
    Cantidad INTEGER, 
    Id_Dieta_FK INTEGER, 
    Id_Proyecto_FK INTEGER, 
    FOREIGN KEY(Id_Proyecto_FK) REFERENCES Proyectos(Id_Proyecto),
    FOREIGN KEY(Id_Dieta_FK) REFERENCES Dietas(Id_Dieta)
);
    
answered by 16.10.2018 в 14:26