Skip to content

Use with Apartment gem leads to wrong sequence being used on postgresql #233

Closed
@ChrisHampel

Description

@ChrisHampel

Our software has 1 PostgreSQL schema per company using our software using the Apartments gem most of the time it works but when there is a lot of activity we will get SQL where the apartment gem is selecting the right schema to insert data into, but this gem is saying to use the wrong schema's sequence for the primary key.

For example, we will be doing an import into schema 3 and it will look up the sequence for schema 1 which was used recently by another company

This either results in:

  • a very large id in a schema where barely any records exist
  • "PG::UniqueViolation: ERROR: duplicate key value violates unique constraint"
    neither of which is preferable since the first will lead to the second eventually unless we intervene and the other just stops saving of the new record all together.

It appears that the sequence being passed in here

connection_memo.next_value_for_sequence(sequence_name)
can be for the wrong schema.

For background information Apartments sets the current schema using the search_path and setting it to:

  • #{schema_name}, public

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions