我有一个 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/