Skip to content

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

Open
@jelofson

Description

@jelofson

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']));
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions