I have two tables generated with the following SQL
static const String createTableFarmersSQL =
"CREATE TABLE Farmers (id INTEGER PRIMARY KEY,"
"createdAt TEXT,"
"updatedAt TEXT,"
"name TEXT,"
"city TEXT,"
"email TEXT,"
"phoneNumber INTEGER,"
"description TEXT);";
static const String createTableFormsSQL=
"CREATE TABLE Forms (id INTEGER PRIMARY KEY,"
"idFarmer INTEGER, "
"createdAt TEXT,"
"updatedAt TEXT,"
"name TEXT,"
"type TEXT,"
"crop TEXT,"
"finished NUMERIC,"
"FOREIGN KEY(idFarmer) REFERENCES Farmers (id));";
And they are launched using the following function:
Future<Database> _openDB() async
{
return openDatabase(join(await getDatabasesPath(),"SILAGEAPPDB6.db"),
onCreate: (db,version)
async {
await db.execute(Constants.createTableFarmersSQL);
await db.execute(Constants.createTableFormsSQL);
}, version: 2
);
}
The tables are created and no error appears, however, the "idFarmers" field of the "Forms" table is not recognized as a foreign key.
Here you can see how the icon that represents that the field is a foreign key does not appear. Apparently I have the SQL syntax right, so I don't know where the error is.
You may have to enable the option that allows use
foreign keys
insqlite
. The functionopenDataBase
has a parameteronConfigure
, in this parameter you will have to pass a command to enable the restrictionforeign key
:Once this change is made, you may have to change the version of the database, or simply leave it at version 1 and uninstall and install the application again.