SQLAlchemy – Querying with DateTime columns to filter by month/day/year

SQLAlchemy effectively translates your query expressed in Python into SQL. But it does that at a relatively superficial level, based on the data type that you assign to the Column when defining your model. This means that it won’t necessarily replicate Python’s datetime.datetime API on its DateTime construct – after all, those two classes are … Read more

Alembic: How to add unique constraint to existing column

To add, you’d need: https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.create_unique_constraint from alembic import op op.create_unique_constraint(‘uq_user_name’, ‘user’, [‘name’], schema=”my_schema”) To drop, you’d need: https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.drop_constraint op.drop_constraint(‘uq_user_name’, ‘user’, schema=”my_schema”)

SQLAlchemy ordering by count on a many to many relationship

I haven’t used SQLAlchemy much so I figured I’d give it a shot. I didn’t try to use your models, I just wrote some new ones (similar enough though): likes = db.Table(‘likes’, db.Column(‘user_id’, db.Integer, db.ForeignKey(‘user.id’)), db.Column(‘post_id’, db.Integer, db.ForeignKey(‘post.id’)) ) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20)) def __repr__(self): return “<User(‘%s’)>” % self.username class … Read more

How to use Enum with SQLAlchemy and Alembic?

Why real values in DB are ‘DRAFT’, ‘APPROVE’, ‘PUBLISHED’, but not draft, etc? I supposed there should be ENUM values, not names. As Peter Bašista’s already mentioned SQLAlchemy uses the enum names (DRAFT, APPROVE, PUBLISHED) in the database. I assume that was done because the enum values (“draft”, “approve”, …) can be arbitrary types in … Read more

Using JSON Type with Flask-sqlalchemy & Postgresql

Looking at the SQLAlchemy documentation for the JSON data type it appears that you should be able to use the .cast method: from sqlalchemy.types import Integer from app import app, db from models import Target # SQLAlchemy 1.1+ data = Target.query.order_by(Target.product[‘salesrank’].astext.cast(Integer)) # SQLAlchemy < 1 data = Target.query.order_by(Target.product[‘salesrank’].cast(Integer))

Where can I find a list of the Flask SQLAlchemy Column types and options?

I think you’re looking for the Column and Data Types page in the documentation. A little HTML parsing gives: ARRAY BIGINT BINARY BLOB BOOLEAN BigInteger Boolean CHAR CLOB Concatenable DATE DATETIME DECIMAL Date DateTime Enum FLOAT Float INT INTEGER Integer Interval LargeBinary MatchType NCHAR NVARCHAR Numeric PickleType REAL SMALLINT SchemaType SmallInteger String TEXT TIME TIMESTAMP … Read more

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table

I just got done setting up a Flask app and I dealt with this kind of problem. I strongly suspect the problem here is that the instance of db that you are creating in __init__.py is unaware of the contents of models.py, including the User class. The db object in __init__.py is a totally separate … Read more

tech