Pylons、SQlite 和自动增量字段

发布于 2024-10-09 23:42:15 字数 870 浏览 1 评论 0原文

嘿! 刚刚开始将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唔猫 2024-10-16 23:42:15

尝试在传统(非声明性)数据定义样式中包含一个 __table_args__ 属性,其中包含要传递给 Table 构造函数的参数,例如:

class Person(Base):
    __tablename__ = "person"
    __table_args__ = {'sqlite_autoincrement': True}

如果必须包含多个参数,请改用此形式(字典必须放在最后):

__table_args__ = (
    Unique('foo'),
    # ...
    {'sqlite_autoincrement': True}
)

来自 表声明式 SQLAlchemy 文档的配置部分:

除名称、元数据和映射的Column 参数之外的表参数是使用__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}
)

From the Table configuration section of the Declarative SQLAlchemy documentation:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文