Skip to content

Foreign Keys

Column-based constraints

$t->bigInt('user_id')
    ->index('posts_user_id_index')
    ->constrained('users', 'id', 'posts_user_id_foreign')
    ->cascadeOnDelete();

Foreign column builder

$t->foreign('author_id')
    ->uuid()
    ->references('users', 'uuid')
    ->cascadeOnDelete();

Actions

->cascadeOnDelete()
->cascadeOnUpdate()
->restrictOnDelete()
->restrictOnUpdate()
->nullOnDelete()
->nullOnUpdate()

Diffing

Schemage supports:

  • adding missing foreign keys
  • dropping removed foreign keys
  • replacing changed foreign keys with DropForeignKey + AddForeignKey

Dropping a foreign key removes an integrity rule but does not directly delete table rows, so it does not require --force. The plan still displays the drop explicitly.

SQLite

SQLite does not support adding or dropping a foreign key with a direct ALTER TABLE statement. The SQLite operation planner converts the affected changes into a table recreation.

The recreated table is built with the final constraints, compatible data is copied, the original table is replaced, and secondary indexes are recreated.