laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.32k stars 10.95k forks source link

Dropping a primary key in PostgreSQL does not take into account the table prefix #48189

Closed ChrisExP closed 1 year ago

ChrisExP commented 1 year ago

Laravel Version

10.20.0

PHP Version

8.2.2

Database Driver & Version

No response

Description

When you create a primary key index in Postgres it is given the name "{$table}_pkey". This is added by postgres so the name has the table prefix automatically.

When you drop the primary key, the prefix is not added to the SQL so it fails. The code is in src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php line 430:

    public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
    {
        $index = $this->wrap("{$blueprint->getTable()}_pkey");

        return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$index}";
    }

The wrap method does not automatically add the prefix to the string. It requires a second argument ($prefixAlias = true) I think. But I'm not to confident with this code, so I don't know what sort of effect that will have. There may be a better way to do it.

Steps To Reproduce

  1. Start a new Laravel project
  2. Add a prefix to the database config
  3. Connect it to a postgres DB
  4. Run a migration to generate a table with a primary key column
  5. Run another migration to remove the primary key
dyriavin commented 1 year ago

Hey 👋

I was trying to reproduce this issue, but no luck s far.

Can you please share your migration code + version of Postgres ? Thank you !

crynobone commented 1 year ago

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

ChrisExP commented 1 year ago

Thanks for looking into it.
I have been able to reproduce it in this repository: https://github.com/ChrisExP/bug-report

Just hook that up to a postgres DB and run php artisan migrate. The last migration will fail when it tries to drop the primary key in the test table.

I don't think this is relevant, but I will mention it just in case. I am on an M1 mac, and Postgres is still not natively supported on Homestead-ARM, so I have been using Docker with Rosetta to use Postgres locally.

github-actions[bot] commented 1 year ago

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

driesvints commented 1 year ago

I can confirm this but can't immediately find a good solution. Would appreciate any help here. Using $prefixAlias on wrap does not solve it.

dyriavin commented 1 year ago

@driesvints Please let me know if my solution works 👀

crynobone commented 1 year ago

Hey there,

v10.21.1 with the relevant changes has been released, please try again and open up a new issue if you're still experiencing this problem.