Skip to content

Commit

Permalink
avoid sqla polymorphic errors
Browse files Browse the repository at this point in the history
When polymorphic modules are declared in database, but absent from venv,
this raise a sqlalchemy polymorphic error. This commit fallback missing
polymorphic identities on default "base" identity.
  • Loading branch information
bouttier committed Nov 13, 2023
1 parent c6bbd85 commit 66a6024
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/geonature/core/gn_commons/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import os
from pathlib import Path
from collections import defaultdict

from flask import current_app
from sqlalchemy import ForeignKey
Expand Down Expand Up @@ -77,10 +78,19 @@ class TModules(DB.Model):
__tablename__ = "t_modules"
__table_args__ = {"schema": "gn_commons"}

"""
Avoid polymorphic error when polymorphic identities are declared
in database but absent from venv: fallback on base identity.
"""
class base_defaultdict(defaultdict):
def __missing__(self, key):
return self["base"]

Check warning on line 87 in backend/geonature/core/gn_commons/models/base.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/gn_commons/models/base.py#L87

Added line #L87 was not covered by tests

type = DB.Column(DB.Unicode, nullable=False, server_default="base")
__mapper_args__ = {
"polymorphic_on": "type",
"polymorphic_identity": "base",
"_polymorphic_map": base_defaultdict(),
}

id_module = DB.Column(DB.Integer, primary_key=True)
Expand Down

0 comments on commit 66a6024

Please sign in to comment.