定向塔,SQlite和自动增量字段 [英] Pylons, SQlite and autoincrementing fields
问题描述
嘿! 刚开始与SQLAlchemy一起使用Pylons,我的模型如下所示:
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
为避免sqlite重用可能已删除的ID,我想将AUTOINCREMENT添加到"id"列中.我仔细阅读了sqlalchemy的文档,发现可以发出sqlite_autoincrement. 可以找到这里.
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似乎是在创建表本身时发出的,我只是想知道当使用声明性样式的模型(例如mine)时如何提供它.
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.
推荐答案
尝试将__table_args__
属性与您将以传统(非声明性)数据定义样式传递给Table
构造函数的参数一起使用,例如:
Try including a __table_args__
attribute with the arguments you would pass to Table
constructors in the traditional (non-declarative) data definition style, e.g.:
class Person(Base):
__tablename__ = "person"
__table_args__ = {'sqlite_autoincrement': True}
如果必须包含多个参数,请改用这种形式(字典必须在最后):
If you have to include several arguments, use this form instead (dict has to be last):
__table_args__ = (
Unique('foo'),
# ...
{'sqlite_autoincrement': True}
)
从表配置部分声明性的SQLAlchemy文档:
From the Table configuration section of the Declarative SQLAlchemy documentation:
使用__table_args__
类属性指定除名称,元数据和映射的Column
自变量以外的表自变量.此属性可容纳通常发送到
Table
构造函数的位置参数和关键字参数.
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.
这篇关于定向塔,SQlite和自动增量字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!