加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Pylons,SQlite和自动增量领域

发布时间:2020-12-12 19:05:11 所属栏目:百科 来源:网络整理
导读:嘿! 刚刚开始与Pylons一起使用SQLAlchemy,我的模型看起来像这样: from sqlalchemy import Columnfrom sqlalchemy.types import Integer,Stringfrom helloworld.model.meta import Baseclass Person(Base): __tablename__ = "person" id = Column(Integer,pr
嘿!
刚刚开始与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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读