Skip to content

Cannot delete or update a parent row: a foreign key constraint fails #2

@mattmattox

Description

@mattmattox

I run into the following issue when using MySQL 5.7 and MariaDB 10.4

Error

go test -v --run TestFindAllUsers
We are connected to the mysql database
=== RUN   TestFindAllUsers
2020/03/05 11:23:21 Error 1217: Cannot delete or update a parent row: a foreign key constraint fails
exit status 1

Workaround

Disable foreign_key_checks before dropping the table then re-enable them.

        server.DB.Exec("SET foreign_key_checks=0")
        err := server.DB.Debug().DropTableIfExists(&models.User{}).Error
        if err != nil {
                return err
        }
        server.DB.Exec("SET foreign_key_checks=1")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions