微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,我想将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 the Table constructor.

原文地址:https://www.jb51.cc/sqlite/197610.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐