【问题标题】:BigInteger in SQLAlchemy or not?SQLAlchemy 中的 BigInteger 与否?
【发布时间】:2011-07-18 15:02:01
【问题描述】:

如果格式不正确,我提前道歉;对我来说已经很晚了。

基本上,我将 Python 与 SQLAlchemy 结合使用。我正在尝试使用Object Relational Mapper, declarative style 将一个类映射到 PostgreSQL 数据库表。

根据SQLAlchemy's documentation on data types,我应该能够使用BigInteger 类型来表示数据库中潜在的大整数,特别是因为我知道PostgreSQL supports the BIGINT data type

所以,我尝试像这样声明我的类:

import sqlalchemy
from sqlalchemy import Column, BigInteger, Text, Sequence, Boolean
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Account(Base):
    __tablename__ = 'accounts'
    __metadata__ = Base.metadata

    id = Column(BigInteger, Sequence('id_seq'), unique=True, nullable=False)
    email = Column(Text(32), unique=True, nullable=False)

    def __init__(self, email):
        self.email = email

但是,当我尝试使用这个文件时,我收到了以下信息:

Traceback (most recent call last):
  File "sqltest02.py", line 9, in <module>
     from account import Account
  File "/home/pdusen/prog/account.py", line 2, in <module>
    from sqlalchemy import Column, BigInteger, Text, Sequence, Boolean
ImportError: cannot import name BigInteger

因此,根据 SQLAlchemy 的文档,BigInteger 类型存在,但根据 python,它不存在。这里有什么我想念的吗?

提前感谢所有答案。

【问题讨论】:

  • 很可能您使用的是旧版本的 SQLAlchemy,它不支持 BigInteger,但阅读文档以获得较新的版本。
  • 你是对的。当然,事情就是这么简单。当然,现在我遇到了一个完全不同的问题(字符串编码......呃)。感谢您的评论。

标签: python postgresql sqlalchemy biginteger


【解决方案1】:

至少从 SQL Alchemy 0.6 起就已支持此功能。正如 cmets 中所述,实际问题是版本太旧。

【讨论】:

  • 我想从技术上讲我应该接受这个作为答案,虽然我并不为这个话题起死回生而自豪......
  • (只是想确保旧问题得到答案,以便其他人以后再次出现时可以参考。我现在正在处理只有一年前的问题。)
猜你喜欢
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 2021-07-13
相关资源
最近更新 更多