Question

I have an already existing MySQL DB with many columns defined with MySQL specific column types (MEDIUMINT, TINYINT to mention a few), and even if the column type belongs to a standard SQL data type, sometimes is declared as unsigned.

Now I'm writing a Flask app to provide an API to access DB in various ways. Using plain SQLAlchemy, I would import specific data types definitions from sqlalchemy.dialects.mysql, like:

from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.mysql import MEDIUMINT, TINYINT

Base = declarative_base()

class User(Base):
    id = Column(MEDIUMINT(unsigned=True), primary_key=True)

But I was told to use Flask-SQLAlchemy, so I'm declaring my tables like

from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer**(???)**, primary_key=True)

How can I tell Flask-SQLAlchemy that the column id is a MySQL MEDIUMINT and that is unsigned? Can I pass, as the first argument to "db.Column" a data type definition imported not from "db.Something" but from sqlalchemy.dialects.mysql, and that would be a good practice (doesn't seem to me)?

Thank you ;)

Was it helpful?

Solution

What's the problem with?

from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import Column
from sqlalchemy.dialects.mysql import MEDIUMINT, TINYINT

db = SQLAlchemy(app)

class User(db.Model):
    id = Column(MEDIUMINT(unsigned=True), primary_key=True)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top