Skip to content

reflection on postgres can cause wrong sequence names #1071

@sqlalchemy-bot

Description

@sqlalchemy-bot

Migrated issue, originally created by Anonymous

If you rename a sequence in postgres, it's kind of weird...

because of transactional issues, the sequence retains its initiallly created id in 'select * from %(sequencename)' , however it is correctly manipulated with the new name.

during reflection ( and possibly during normal table setup? i did not test ), SqlAlchemy pulls the original id for the sequence , i do not know from where.

SqlAlchemy should pull the current sequence name instead.

The only way to 'sync' both ids, is to dump db, drop db, reload.

This means that renaming any sequence in postgres ( granted, not a common task) seems to break sqlalchemy integration.

the response i got last week from -
http://archives.postgresql.org/pgsql-general/2008-05/msg00961.php

some older relevant pg threads:
http://archives.postgresql.org/pgsql-hackers/2008-03/msg00008.php
http://archives.postgresql.org/pgsql-hackers/2002-02/msg01095.php

This is a walkthrough of the phenomena.

=> CREATE TABLE sample_table ( id bigserial primary key not null );
NOTICE: CREATE TABLE will create implicit sequence "sample_table_id_seq" for serial column "sample_table.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "sample_table_pkey" for table "sample_table"
CREATE TABLE

=> \d sample_table;
Table "public.sample_table"
Column | Type | Modifiers
--------+--------+----------------------------------------------
id | bigint | not null default nextval('sample_table_id_seq'::regclass)

=>select * from sample_table_id_seq;
sequence_name | last_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
---------------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
sample_table_id_seq | 1 | 1 | 9223372036854775807 | 1 | 1 | 1 | f | f
(1 row)

=> alter table sample_table_id_seq rename to id_seq ;
=> \d sample_table;
Table "public.sample_table"
Column | Type | Modifiers
--------+--------+----------------------------------------------
id | bigint | not null default nextval('id_seq'::regclass)

=> select * from id_seq;
sequence_name | last_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
---------------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
sample_table_id_seq | 1 | 1 | 9223372036854775807 | 1 | 1 | 1 | f | f


Attachments: sqlalchemy-seq-fix.diff

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions