Skip to content
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

MySQL DropForeignKey only works if the referenced column in same database #2069

Open
jelofson opened this issue Feb 7, 2022 · 3 comments
Open
Labels

Comments

@jelofson
Copy link

jelofson commented Feb 7, 2022

It is possible to have a foreign key that points to a table in a different database (albeit not common?). In this case, the drop foreign key will fail, as the query looks for key usage in "DATABASE()". See snippet below from MysqlAdapter about line 898.

I think, but am not sure, that you could modify the WHERE condition to IS NOT NULL? For context, I have an employees database that is separate from an application database. The application database references employees in that employees DB. The workaround for me would be to create a view. So, I would not consider this a high priority issue, but one you may want to know about. The juice might not be worth the squeeze, so to speak.

foreach ($columns as $column) {
            $rows = $this->fetchAll(sprintf(
                "SELECT
                    CONSTRAINT_NAME
                  FROM information_schema.KEY_COLUMN_USAGE
                  WHERE REFERENCED_TABLE_SCHEMA = DATABASE()
                    AND REFERENCED_TABLE_NAME IS NOT NULL
                    AND TABLE_NAME = '%s'
                    AND COLUMN_NAME = '%s'
                  ORDER BY POSITION_IN_UNIQUE_CONSTRAINT",
                $tableName,
                $column
            ));

            foreach ($rows as $row) {
                $instructions->merge($this->getDropForeignKeyInstructions($tableName, $row['CONSTRAINT_NAME']));
            }
        }
@MasterOdin MasterOdin added the bug label Feb 18, 2022
@JRDuncan
Copy link
Contributor

@jelofson Was this foreign key created by phinx?

@jelofson
Copy link
Author

It's been a year, so I don't really remember, but probably. I don't think it should matter.

@JRDuncan
Copy link
Contributor

JRDuncan commented Mar 2, 2023

I don't think phinx allows interacting with databases outside of the database specified in the connection. Based on what I've seen it doesn't handle databases or schemas through migrations unless we use raw SQL. This would be a great feature tho

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants