// sqlite · sql behavior

defer_foreign_keys

Postpone foreign key checks to COMMIT for the current transaction.

默认
0 (off)
Range
0, 1
Restart needed
否
Default checked on
SQLite 3.5x

作用

Resets to off at the end of every transaction. Useful when rows must be inserted in an order that violates constraints temporarily.

默认值

0 (off). Off (a stock SQLite 3.5x build).

Check the current value

PRAGMA defer_foreign_keys;

SELECT * FROM pragma_defer_foreign_keys;   -- table-valued form, usable in queries

Change it

PRAGMA defer_foreign_keys = ON;   -- this connection only

No restart. It applies at once to the connection that runs it and is lost when that connection closes, so set it every time a connection opens.

When to change it

Inside a transaction that loads or reorders related rows.

What can go wrong

The violation is reported only at COMMIT, which then fails.

Questions about defer_foreign_keys

What is the default value of defer_foreign_keys in SQLite?

0 (off). Off (a stock SQLite 3.5x build).

Does changing defer_foreign_keys require a restart?

No. No restart. It applies at once to the connection that runs it and is lost when that connection closes, so set it every time a connection opens.

When should I change defer_foreign_keys?

Inside a transaction that loads or reorders related rows.

Back to all SQLite settings, or check the SQLite manual for every option.

// one client for every engine

Check defer_foreign_keys on your own server

SQLly runs the query above against SQLite and the other engines in this reference, from one desktop app on macOS, Windows and Linux.