- My
Test
program connects to the database, defines thePRAGMA
, and (always) deletes the country ‘BE’. Nothing else… - I don’t use transactions for this
Test
program
This is a dump of this simple database:
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "tbl_Cities" (
"fld_Country" TEXT,
"fld_Zip" TEXT,
"fld_City" TEXT,
FOREIGN KEY("fld_Country") REFERENCES "tbl_Countries"("fld_ShortName") On Delete Restrict,
PRIMARY KEY("fld_Country","fld_Zip")
);
CREATE TABLE IF NOT EXISTS "tbl_Countries" (
"fld_ShortName" TEXT,
"fld_LongName" TEXT,
PRIMARY KEY("fld_ShortName")
);
INSERT INTO "tbl_Cities" VALUES ('BE','2000','Brussels');
INSERT INTO "tbl_Countries" VALUES ('BE','Belgium');
COMMIT;
And this is the code used in xojo test
program:
Var sSQL As String
sSQL = "DELETE FROM tbl_Countries WHERE fld_shortName = 'BE'"
Try
db.ExecuteSQL(sSQL)
MessageBox ("Deleted")
Catch err As DatabaseException
MessageDialog.Show("Fout " + err.ErrorNumber.ToString)
End Try
I will create a new version of this database based on the dump
Why a PK like this? SQLite have some “behaviors” when using compound indexes.
Probably you don’t use the correct way to enable the pragma foreign_keys
once connected use: db.ExecuteSQL (“PRAGMA foreign_keys = on”) and you will get the expected result.
I use really often compound primary keys in SQLite and never had any problems
The combination of fld_Country
and fld_Zip
creates a unique primary key.
Not exactly a problem Antonio, but in many cases we need separated indexes. In the above case, if you search for the field fld_zip, SQLite will do sequential search instead of a indexed one, for example. You will always need both fields or it will not use the index.
And as Antonio pointed out, see if the pragma will activate the constraint.
This PRAGMA statement was already in the code immediately after connecting to the database
Just out of curiosity, check if something changes if you create an index just for that field in question.
CREATE TABLE IF NOT EXISTS "tbl_Cities" (
"fld_Country" TEXT,
"fld_Zip" TEXT,
"fld_City" TEXT,
FOREIGN KEY("fld_Country") REFERENCES "tbl_Countries"("fld_ShortName") On Delete Restrict,
PRIMARY KEY("fld_Country","fld_Zip")
);
CREATE INDEX idx_cities_country ON tbl_Cities(fld_Country);
@TimStreater @Antonio_Rinaldi @Javier_Menendez
Splendid news… I dumped the old database and recreated a new database with the dumped SQL sentences. The xojo program connected to the new database now catches the Delete
error (error code 19).
One day lost to solve this problem… Thank you all for your help.
Well, you had a legacy DB structure fail. Good to know.