可能在SQLAlchemy列中设置了不正确的自动增量

时间:2016-08-26 12:13:09

标签: python sqlalchemy

我有这些SQLAlchemy类:

Base = declarative_base()
metadata = Base.metadata

class Stock(Base):
    __tablename__ = 'stock'
    __table_args__ = {'schema': 'jm_sa'}

    date = Column(DateTime, primary_key=True, autoincrement=False)
    center = Column(Integer, primary_key=True, autoincrement=False)
    sku = Column(Integer, primary_key=True, autoincrement=False)
    qty = Column(Integer)


class StockFull(Base):
    __tablename__ = 'stock_full'
    __table_args__ = {'schema': 'jm_sa'}

    id = Column(Integer, primary_key=True, autoincrement=True)
    full_date = Column(DateTime)
    center = Column(Integer)
    sku = Column(Integer)
    fc = Column(Numeric)
    stock_total = Column(Numeric)

当我尝试获取非自动增量股票的所有列时:

[x.name for x in Stock.__table__.columns if not x.autoincrement]

我明白了:

['date', 'center', 'sku'] # i.e. all but ['qty']

和StockFull:

[x.name for x in StockFull.__table__.columns if not x.autoincrement]

我明白了:

[] # i.e. Are they all autoincrement?

对于StockFull.id列,不应该自动增量为真吗?

注意: 我能用这段代码得到我想要的东西,但_autoincrement_column被标记为受保护,所以我认为这不是最好的方法:

[x.name for x in StockFull.__table__.columns
    if StockFull.__table__._autoincrement_column is None or
        x != StockFull.__table__._autoincrement_column]

0 个答案:

没有答案