From 716ad75dbc13bb8559d65c808a61c5e5c9afbbd9 Mon Sep 17 00:00:00 2001 From: Parnassius Date: Sat, 11 Dec 2021 16:18:11 +0100 Subject: [PATCH] Use `viewonly=True` to suppress relationship warnings in SQLA 1.4 --- pokedex/db/multilang.py | 1 + pokedex/db/tables.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/pokedex/db/multilang.py b/pokedex/db/multilang.py index 6d2f242..211df50 100644 --- a/pokedex/db/multilang.py +++ b/pokedex/db/multilang.py @@ -185,6 +185,7 @@ def create_translation_table(_table_name, foreign_class, relation_name, foreign_keys=[Translations.foreign_id, Translations.local_language_id], uselist=False, lazy=relation_lazy, + viewonly=True, )) # Add per-column proxies to the original class diff --git a/pokedex/db/tables.py b/pokedex/db/tables.py index e2f7a20..e30d54b 100644 --- a/pokedex/db/tables.py +++ b/pokedex/db/tables.py @@ -30,6 +30,7 @@ from functools import partial import six from sqlalchemy import Column, ForeignKey, MetaData, PrimaryKeyConstraint, UniqueConstraint +from sqlalchemy import __version__ as sqla_version from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.hybrid import hybrid_property @@ -39,6 +40,12 @@ from sqlalchemy.types import Boolean, Enum, Integer, SmallInteger, Unicode, Unic from pokedex.db import markdown, multilang +relationship = partial(relationship, viewonly=True) +if (1, 3, 17) <= tuple(int(x) for x in sqla_version.split(".")) < (1, 4): + # `sync_backref` was introduced in 1.3.17 + # Since 1.4 it defaults to False if `viewonly` is True + relationship = partial(relationship, sync_backref=False) + class TableSuperclass(object): """Superclass for declarative tables, to give them some generic niceties like stringification.