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

Example using BLOB in SQLAlchemy

from sqlalchemy import * from sqlalchemy.orm import mapper, sessionmaker import os engine = create_engine(‘sqlite://’, echo=True) metadata = MetaData(engine) sample = Table( ‘sample’, metadata, Column(‘id’, Integer, primary_key=True), Column(‘lob’, Binary), ) class Sample(object): def __init__(self, lob): self.lob = lob mapper(Sample, sample) metadata.create_all() session = sessionmaker(engine)() # Creating new object blob = os.urandom(100000) obj = Sample(lob=blob) session.add(obj) session.commit() … Read more

Is there any way to generate sequential Revision IDs in Alembic?

By the sounds of it, you are more interested in sequentially listed revision files rather than sequentially ordered revision ids. The former can be achieved without any change to how the revision ids are generated. The alembic.ini file that is generated when you run alembic init alembic has a section that configures the naming of … Read more

How to declare a table class that contains multi-column primary key?

In case columns are declared in the same order as they should be in the primary key: class User(Base): field1 = Column(Integer, primary_key=True) field2 = Column(Integer, primary_key=True) Otherwise declare it in __table_args__: class User(Base): field1 = Column(Integer) field2 = Column(Integer) __table_args__ = ( PrimaryKeyConstraint(field2, field1), {}, )

How do I get the name of an SQLAlchemy object’s primary key?

If using SQLAlchemy version 0.8 or later, you should use the runtime inspection API. If the model class is User and the sole primary key is id, >>> from sqlalchemy.inspection import inspect >>> inspect(User).primary_key[0].name ‘id’ If the model class is User and there are many primary keys, >>> from sqlalchemy.inspection import inspect >>> [key.name for … Read more

SQLAlchemy 0.5.8 Max function

I know this question is specifically about v0.5.8, but for anyone coming here from google 4 years later, func is now in sqlalchemy.sql.expression. Example: from sqlalchemy.sql.expression import func session.query(func.max(Table.column))

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)