Skip to content

Schemas with the same table name can not be synced with constraints #15639

Closed as not planned

Description

@dspintelegence

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

If I have two different schemas in my PostgreSQL database and they both share a table with the same name. The sequelize.sync({ force: true }) command will try to delete the constraints from the other table. This always leads to an error that states: parent: error: relation "schema_name.table_name" does not exist.

Reproducible Example

Here is the link to the SSCCE for this issue:
Schemas with the same table name can not be synced with constraints

What do you expect to happen?

Constraints should only be dropped, If the current schema has them associated. Besides the name of the constraints, the namespace should also be checked.

What is actually happening?

The sql output states to delete the constraints:

'ALTER TABLE "schema_name"."table_name" DROP CONSTRAINT "table_name_number_id_fkey"'.

The constraint table_name_number_id_fkey does not exist in my current schema but in the other schema.

Environment

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity

  1. added
    pending-approval
    type: bug
    on Feb 2, 2023
  2. ephys commented on Feb 3, 2023

    @ephys
    Member

    You're defining two different models with the same name, User. The second model overwrites the first one so the model for schema_a doesn't exist in sequelize.models anymore, and does not get synched

    This is something I plan on banning eventually, all model names must be unique

  3. dspintelegence commented on Feb 8, 2023

    @dspintelegence
    Author

    Hello @ephys thanks for your answer.
    Unfortunately the sscce repo does not allow me to picture our case precisely. Here is more information about what is happening:

    We are working with a microservice architecture of multiple node.js backends. Each backend creates tables and saves data dedicated to their own schema in a single database (multi-tenant service postgreSQL database).

    If in two microservices there is a model with the same name, there is no direct way of telling if it is duplicated, because sequelize.sync({ force: true }) is called for each service respectively. Besides that, this is not our issue here.

    The real issue is, that constraints from one schema are deleted by the sequelize instance in another service with another schema, if the model name is the same. This should not be possible, because constraints are created for a specific schema.

    The solution for this case should be that only constraints from the current schema should be dropped, not all constraints that follow a certain model name, found my sequelize. Otherwise we would drop constraints from other apps running in our database which is crucial.

  4. ephys commented on Mar 2, 2023

    @ephys
    Member

    force drops the table before creating it, if multiple instances are touching the same table, they will fully reset them

  5. github-actions commented on Mar 17, 2023

    @github-actions
    Contributor

    This bug report has not been verified by maintainers yet, and has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, consider commenting with more information to help us verify it.

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

    pending-approvalstaletype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions