Pylons、SQlite 和自动增量字段
嘿! 刚刚开始将 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,我想将 AUTOINCRMENT 添加到“id”列。我浏览了 sqlalchemy 的文档,发现可以发出 sqlite_autoincrement 。 可以找到给出此属性的示例 这里。
sqlite_autoincrement 似乎是在创建表本身时发出的,我只是想知道在使用模型的声明式风格(例如我的模型)时如何提供它。
Hey!
Just started working with Pylons in conjunction with SQLAlchemy, and my model looks something like this:
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
To avoid sqlite reusing id's that might have been deleted, I want to add AUTOINCREMENT to the column "id". I've looked through the documentation for sqlalchemy and saw that the sqlite_autoincrement can be issued.
An example where this attribute is given can be found here.
sqlite_autoincrement seems though to be issued when creating the table itself, and I just wondered how it can be supplied when using a declarative style of the model such as mine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在传统(非声明性)数据定义样式中包含一个 __table_args__ 属性,其中包含要传递给
Table
构造函数的参数,例如:如果必须包含多个参数,请改用此形式(字典必须放在最后):
来自 表声明式 SQLAlchemy 文档的配置部分:
Try including a
__table_args__
attribute with the arguments you would pass toTable
constructors in the traditional (non-declarative) data definition style, e.g.:If you have to include several arguments, use this form instead (dict has to be last):
From the Table configuration section of the Declarative SQLAlchemy documentation: