Skip to content

migrate:fresh fails to drop tables due to FK constraints on mysql #913

@elgow

Description

@elgow

Describe the bug
migrate:fresh fails to drop tables because of FK constraints on mysql. This is true both with and w/o the -f option.

To Reproduce
Steps to reproduce the behavior:

  1. On a mysql DB, create a migration that defines a schema with two tables A and B where B has a FK on A with FK constraint
        with self.schema.create("A") as table:
            table.increments("rowid").primary()

        with self.schema.create("B") as table:
            table.increments("rowid").primary()
            table.unsigned_integer('a_id', nullable=False)
            table.foreign('a_id').references('rowid').on('A')

  1. Run masonite-orm migrate:fresh (will succeed)
  2. Run masonite-orm migrate:fresh (will fail, see below)
  3. See error

Expected behavior
Should delete all tables and re-create the schema

Actual results
"Cannot drop table 'A' referenced by a foreign key constraint 'b_a_id_foreign' on table 'B'.")

Desktop (please complete the following information):

  • OS: Rocky linux
  • Version: "8.6 (Green Obsidian)"

What database are you using?

  • Type: mysql
  • Version 8.0.30
  • Masonite ORM 2.23.2

Additional context
If you rename A to C so that the alpha order is reversed then it will work. It seems that the table deletion is done in alpha order rather than based on a dependency graph. The -f argument fails to disable the FK constraints so that the table drop may proceed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn existing feature is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions