python - 如何使用 SQLAlchemy 声明此列

标签 python mysql sqlalchemy

我有一个 MySQL 表,其中一列是小数。它是使用以下参数创建的:

 `ratio` decimal(5,3) DEFAULT NULL,

到目前为止,我整理的示例脚本非常简单。

import sys 
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String

Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()

class DailyProjections(Base):
  __tablename__ = 'daily_projections'

  id = Column(Integer, primary_key=True)
  name = Column(String(100))

如何在此处声明比率列?

最佳答案

来自docs :

The Numeric type is designed to receive data from a database type that is explicitly known to be a decimal type (e.g. DECIMAL, NUMERIC, others) and not a floating point type (e.g. FLOAT, REAL, others).

所以你这样声明:

ratio = Column(Numeric(5, 3), nullable=True)

关于python - 如何使用 SQLAlchemy 声明此列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31211188/

相关文章:

python - 在sqlalchemy中设置事件时区分插入和更新

Python 拼写检查和建议

python - Xlsxwriter 对同一列中不同单元格的不同格式

python - 不清楚身份验证后如何调用 API?

php - MySQL 查询将字段设置为 0 而不是空白字符串

python - 通过 Flask SQLAlchemy 从 SQLite 读取 BLOB (LargeBinary) 返回 None

python - Axes_Grid1 : Ticks, 刻度标签和标签

mysql - 是否可以 count() 一个不同查询的子集?

java - 如何让spring使用mysql而不是hsql

sqlalchemy - 如何按距查询点的距离对地理空间查询进行排序