1. Michael Bayer
  2. sqlalchemy
  3. Issues

Issues

Issue #3166 wontfix

autoincrement should not be True for non-integer primary key

lieryan
created an issue

The behavior of autoincrement attribute is misleading when the primary key is not an integer primary key.

Test case:

from sqlalchemy import Column, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class Table(Base):
    __tablename__ = 'table'
    code = Column(String(50), primary_key = True)

table = Base.metadata.tables['table']
pk_column = table.primary_key.columns['code']
assert not pk_column.autoincrement

Comments (3)

  1. Michael Bayer repo owner

    "autoincrement" does not deliver any information about whether or not the column does anything regarding the database's "autoincrement" feature. As the docs state, it defaults to True. It does not "change" its value automatically.

    There's no fix I can see to take here. Can't change its name, can't change its behavior. Making it non-readable would affect applications that rely upon it's setting.

    There is non-public API right now, Table._autoincrement_column, which does actually evaluate the full status of the column.

    There's no action I can see that can be taken here.

  2. Michael Bayer repo owner

    Further complexity:

    create two tables, no primary key

    x = Table('x', metadata, Column('id', Integer, autoincrement=True))
    y = Table('y', metadata, Column('id', Integer, autoincrement=False))
    

    "autoincrement" magically sets itself to False, overriding "True", both have autoincrement=False

    >>> x.c.id.autoincrement
    False
    
    >>> y.c.id.autoincrement
    False
    

    Add a primary key after the fact:

    >>> x.append_constraint(PrimaryKeyConstraint('id'))
    >>> y.append_constraint(PrimaryKeyConstraint('id'))
    

    Now what is autoincrement? It's False on both. How do we know which one to flip back to True? This can only be achieved if we a. leave the recieved value of "autoincrement" as it was, but then the .autoincrement attribute reports the "effective" value, which is then misleading as well or b. add a third state for "autoincrement", to distinguish "the user set it to True but we flipped it to False, vs. it was at its default of True but we flipped it to False".

    Really, all indications here point to leaving autoincrement alone.

  3. Log in to comment