Skip to content

Conversation

@challet
Copy link
Contributor

@challet challet commented Sep 6, 2017

With PostgreSQL driver, the methods Schema::disableForeignKeyConstraints and Schema::enableForeignKeyConstraints are in fact deferring the constraints.

In order for them to be deferred, they need to be deferrable, see https://www.postgresql.org/docs/9.0/static/sql-createtable.html :

Checking of constraints that are deferrable can be postponed until the end of the transaction (using the SET CONSTRAINTS command)

This pull request allows to add such a "DEFERRABLE" option in a migration :

Schema::table('children', function(Blueprint $table) {
  $table
    ->foreign('parent_id')
    ->references('id')
    ->on('parent')
    ->onDelete('cascade')
    ->deferrable('DEFERRABLE INITIALLY IMMEDIATE');
});

Possible values are :

  • NOT DEFERRABLE (Postgres default)
  • DEFERRABLE
  • DEFERRABLE INITIALLY IMMEDIATE (default of the previous line)
  • DEFERRABLE INITIALLY DEFERRED

Allow support of deferrable option in an "add constraint foreign key" statement
@GrahamCampbell GrahamCampbell changed the title add a compileForeign method to PostgresGrammar.php [5.5] Add a compileForeign method to PostgresGrammar Sep 6, 2017
@GrahamCampbell GrahamCampbell changed the base branch from 5.4 to 5.5 September 6, 2017 18:09
@taylorotwell
Copy link
Member

Is there some standard default value we could give this method rather than forcing you to pass a magic string of commands?

@challet
Copy link
Contributor Author

challet commented Sep 7, 2017

Yes, but it would need to call two mehods :

  • ->deferrable(false) NOT DEFERRABLE (which is the Postgres default if nothing specified)
  • ->deferrable(true) or ->deferrable() DEFERRABLE (by Fluent default)
  • ->deferrable(true|empty)->initiallyImmediate(true|empty) DEFERRABLE INITIALLY IMMEDIATE (for Postgres it the default of "DEFERRABLE" alone)
  • ->deferrable(true|empty)->initiallyImmediate(false) DEFERRABLE INITIALLY DEFERRED

Tests have been added to reflect the different cases.

@taylorotwell taylorotwell merged commit b19e491 into laravel:5.5 Sep 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants