Skip to content

Ensure foreign key columns create an index by default and add tests for constrained method behavior #56312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

theparadoxShin
Copy link

This PR fixes the bug mentionned in issu #56266 , enabling web artisans using PostgreSQL to work more quickly without worrying about whether an index has been created or not. All cases of index creation for good performance are handled.

@mathiasgrimm
Copy link
Contributor

I've created a draft PR that is related to this

@theparadoxShin
Copy link
Author

theparadoxShin commented Jul 17, 2025

@mathiasgrimm A lot of configuration I think . I took the road of simplicity by creating automatically index on foreign key by default !

And I saw that you created a new function addAutoForeignKeyIndexes() yet addFluentIndexes() exists .

But I like the idea to say to the framework that you don't want index on a particular faoreign key so I will keep the withoutIndex() , and it can look like this in real implementation : " $table->foreignId('user_id')->constrained()->withoutIndex(); "

Personnaly on my solution I will update the constrained() function like this to add a global configuration :

if (! isset($this->index)) {
    // Verified the config if exist but true by default so we create 
    $autoCreate = $this->blueprint->getConnection()
        ->getConfig('foreign_key_implicit_index_creation', true);
    if ($autoCreate) {
        $this->index = true;
    }
}

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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.

3 participants