嘿!
刚刚开始与Pylons一起使用sqlAlchemy,我的模型看起来像这样:
刚刚开始与Pylons一起使用sqlAlchemy,我的模型看起来像这样:
from sqlalchemy import Column from sqlalchemy.types import Integer,String from helloworld.model.Meta import Base class Person(Base): __tablename__ = "person" id = Column(Integer,primary_key=True) name = Column(String(100)) email = Column(String(100)) def __init__(self,name='',email=''): self.name = name self.email = email def __repr__(self): return "<Person('%s')" % self.name
为了避免sqlite重用可能已被删除的id,我想将AUTOINCREMENT添加到列“id”.我查看了sqlalchemy的文档,看到可以发出sqlite_autoincrement.
给出该属性的示例可以是here.
sqlite_autoincrement似乎是在创建表本身时发出的,我只是想知道在使用模型的声明式样式时如何提供它.
尝试将__table_args__属性包含在传统(非声明性)数据定义样式中传递给Table构造函数的参数中,例如:
class Person(Base): __tablename__ = "person" __table_args__ = {'sqlite_autoincrement': True}
如果你必须包含几个参数,请使用此表单(dict必须是最后一个):
__table_args__ = ( Unique('foo'),# ... {'sqlite_autoincrement': True} )
从Declarative sqlAlchemy文档的Table configuration部分:
Table arguments other than the name,Metadata,and mapped
Column
arguments are specified using the__table_args__
class attribute. This attribute accommodates both positional as well as keyword arguments that are normally sent to theTable
constructor.