【问题标题】:Unsigned float in sqlalchemysqlalchemy中的无符号浮点数
【发布时间】:2018-04-20 20:29:49
【问题描述】:

如何在 python - sqlalchemy 框架中拥有无符号浮点数据类型?

我使用的是整数类型,结果为Warning: Out of range value for column 'xxxx' at row 1 cursor.execute(statement, parameters)

我正在这样实现表格:

class testTable(Base):
    __tablename__ = 'test_table'

    id = Column(Integer, primary_key=True)
    xxxx = Column(Integer, ForeignKey('another.id'))
    yyyy = Column(Integer, nullable=False)
    .
    .
    .

MySQL 是数据库服务器。

【问题讨论】:

    标签: python mysql database sqlalchemy


    【解决方案1】:

    查看documentation 会发现有Float 类型。

    from sqlalchemy import Float
    
    (...)
    
    class testTable(Base):
        __tablename__ = 'test_table'
    
        id = Column(Integer, primary_key=True)
        xxxx = Column(Integer, ForeignKey('another.id'))
        yyyy = Column(Integer, nullable=False)
        zzzz = Column(Float)
    

    有关诸如unsigned 等 MySQL 细节,请参阅 the respective documentation,例如导入特定的 UPPERCASE 类型:

    from sqlalchemy.dialects.mysql import FLOAT
    

    然后您可以像往常一样使用这些来创建您的列:

    zzzz = Column(FLOAT(unsigned=True))
    

    【讨论】:

    • 我正在寻找无符号浮点数
    • 感谢您的回复。我已经浏览了那个链接和那个部分。需要知道如何实现它。
    • @Vijay47 我想这有点容易错过,请参阅编辑。
    • from sqlalchemy.dialects.mysql import FLOAT 会将 FLOAT 类型导入为无符号?
    • @Vijay47 它将导入特定于 mysql 方言的 FLOAT 类型,然后您可以使用适当的选项创建 Column,例如unsigned=Truezerofill=True 等等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多