简体   繁体   English

如何使用 SQLAlchemy 声明此列

[英]How to declare this column with SQLAlchemy

I have a MySQL table where one of the columns is a decimal.我有一个 MySQL 表,其中一列是小数。 It is created with the following parameters:它是使用以下参数创建的:

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

So far the sample scripts I'm putting together is super dumbed down.到目前为止,我放在一起的示例脚本非常简单。

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))

How do I declare the ratio column here?我如何在这里声明比率列?

From the docs :文档

The Numeric type is designed to receive data from a database type that is explicitly known to be a decimal type (eg DECIMAL, NUMERIC, others) and not a floating point type (eg FLOAT, REAL, others). Numeric 类型旨在从明确知道是十进制类型(例如 DECIMAL、NUMERIC 等)而不是浮点类型(例如 FLOAT、REAL 等)的数据库类型接收数据。

So you declare it like so:所以你像这样声明它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM