randombits
randombits

Reputation: 48490

How to declare this column with SQLAlchemy

I have a MySQL table where one of the columns is a decimal. 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?

Upvotes: 2

Views: 250

Answers (1)

doru
doru

Reputation: 9110

From the 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).

So you declare it like so:

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

Upvotes: 2

Related Questions