SQLAlchemy update if unique key exists

From version 1.2 SQLAlchemy will support on_duplicate_key_update for MySQL There is also examples of how to use it: from sqlalchemy.dialects.mysql import insert insert_stmt = insert(my_table).values( id=’some_existing_id’, data=”inserted value”) on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update( data=insert_stmt.values.data, status=”U” ) conn.execute(on_duplicate_key_stmt) From version 1.1 SQLAlchemy support on_conflict_do_update for PostgreSQL Examples: from sqlalchemy.dialects.postgresql import insert insert_stmt = insert(my_table).values( id=’some_existing_id’, data=”inserted value”) do_update_stmt … Read more

SqlAlchemy asyncio orm: How to query the database

session.query is the old API. The asynchronous version uses select and accompanying methods. from sqlalchemy.future import select from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker engine = create_async_engine(_build_async_db_uri(CONFIG.general.sqlalchemy_db_uri)) async_session = sessionmaker( engine, expire_on_commit=False, class_=AsyncSession ) CACHE = {} async def _load_all(): global CACHE try: async with async_session() as session: q = select(TableClass) result = … Read more

Why do I get no such table error when installing Apache Airflow on Mac?

You need to perform initialization after installation: $ export AIRFLOW_HOME=some/dir $ airflow db init # or `airflow initdb` for the legacy 1.X If AIRFLOW_HOME is unset, ~/airflow/ will be created and used. This is where the config and logs will be stored; if you want to reset the configuration, remove the dir stored in AIRFLOW_HOME … Read more

How does `nullable=False` work in SQLAlchemy

The doc you reference explains the issue: This parameter is only used when issuing CREATE TABLE statements. If you originally created your database without nullable=False (or created it in some other way, separate from SQLAlchemy, without NOT NULL on that column), then your database column doesn’t have that constraint information. This information lives in reference … Read more

Type hints for SQLAlchemy engine and session objects

Figured it out: connection_string: str = “sqlite:///:memory:” engine = create_engine(connection_string) session = Session(bind=engine) print(type(engine)) # sqlalchemy.engine.base.Engine print(type(session)) # sqlalchemy.orm.session.Session Thus, type hinting is achieved the following way for example: from sqlalchemy.engine.base import Engine def test_func(engine: Engine): pass

AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy

Edit If you’re experiencing this, upgrading Flask-SQLAlchemy to >= 2.5 should resolve the issue per https://github.com/pallets/flask-sqlalchemy/issues/910#issuecomment-802098285. Pinning SQLAlchemy to ~1.3 should no longer be necessary. I ran into this issue a little earlier, but think I’ve figured out what’s going on. SQLAlchemy is automatically installed as a dependency for Flask-SQLAlchemy and its latest release (1.4.0) … Read more

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