Skip to content

Order SQL exports by foreign key dependency #2517

Description

@datlechin

Problem

A multi-table SQL export writes tables in whatever order the tree lists them. Importing that file into a database with foreign keys enabled fails on the first child row whose parent is not there yet. The Disable foreign key checks option papers over it, but it needs a privilege on PostgreSQL and it is off for engines that ignore it.

Proposed solution

  • Topologically sort tables by foreign key dependency before writing, parents first.
  • On a cycle, fall back to the current order and say so in the export summary rather than writing something that cannot import.
  • Keep the existing behaviour of adding foreign keys with ALTER TABLE ... ADD CONSTRAINT after the data, so the two paths agree.

Alternatives considered

Reorder the file by hand, or disable foreign key checks and hope.

Related database type

PostgreSQL

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions