autogenerate for sequences, as well as create_sequence()/drop_sequence()

Issue #60 new
Anonymous created an issue

No description provided.

Comments (11)

  1. Anonymous

    Try this sqlalchemy declarative base:

    id = db.Column(db.Integer, db.Sequence("s_%s" % __tablename__), primary_key=True)
    

    but alembic revision autogeneration make migration scrip without any sequence.

  2. Michael Bayer repo owner

    autogenerate doesn't look at sequences right now, no.

    there's a lot of TODOs for autogenerate, mainly schema support.

    I will reiterate that you don't need autogenerate or explicit operations methods to use sequences with alembic. this will work right now:

    from sqlalchemy.schema import Sequence, CreateSequence
    op.execute(CreateSequence(Sequence("some_sequence"))
    
  3. Anonymous

    Thank you for advice. I will try to use this example. Sure that autogenerate would have done it much faster than I. Hopefully that this enhancement soon will be.

    Do you think about store upgrade/downgrade migrations scripts in native SQL? I think it will be easy more.

  4. Anton Kazak

    alembic/autogenerate/compare.py:154

    existing_tables = conn_table_names.intersection(metadata_table_names)
    
    existing_metadata = sa_schema.MetaData()
    conn_column_info = {}
    for s, tname in existing_tables:
        name = sa_schema._get_table_key(tname, s)
    

    existing_tables - unsorted set, it's not a bug?
    because in alembic/autogenerate/compare.py:171
    sorted(existing_tables, key=lambda x: (x[0] or '', x[1]))

    when I run alembic revision --autogenerate in console I see logs
    INFO [alembic.ddl.postgresql] Detected sequence named 'user_id_seq' - for example
    but if I run second or third time this sequence not showing, but show 'profile_id_seq' for example

  5. Michael Bayer repo owner

    existing_tables - unsorted set, it's not a bug?

    not that I'm aware of?

    because in alembic/autogenerate/compare.py:171 sorted(existing_tables, key=lambda x: (x[0] or '', x[1]))

    sometimes we want the collection to be sorted, so we call sorted() on it. Sometimes we don't care. We are also sorting by name, not insertion order as a plain "ordered set" usually would be doing.

    when I run alembic revision --autogenerate in console I see logs INFO [alembic.ddl.postgresql] Detected sequence named 'user_id_seq' - for example

    the Postgresql alembic dialect detects sequences that are associated with tables via PG's SERIAL datatype. That's not what this issue covers. General autogen of sequences by themselves is still not implemented. The logic you refer towards only runs when the parent table that contains the SERIAL itself is to be dropped or added.

  6. Log in to comment