SQLite3 Foreign Key Constraints
While using SQLite3, you may encounter an issue where rows can be inserted into a child table even when the parent table is empty. This behavior arises due to SQLite3's default behavior, where foreign key constraints are disabled upon connection.
To enable foreign key constraints in SQLite3, execute the following query every time you connect to the database:
PRAGMA foreign_keys = ON;
The reason for repeating this command every time is to maintain compatibility with SQLite version 2.x. By default, SQLite3 behaves like SQLite 2.x, which ignores foreign key constraints.
In SQLite version 4.x, however, foreign key constraints will be enabled by default, resolving this issue permanently.
The above is the detailed content of Why Do SQLite3 Foreign Key Constraints Need to Be Enabled Explicitly?. For more information, please follow other related articles on the PHP Chinese website!