Skip to content

Commit

Permalink
(fix) sqla polymorphic errors
Browse files Browse the repository at this point in the history
avoid sqla polymorphic errors
  • Loading branch information
jacquesfize authored Dec 11, 2023
2 parents 4465b13 + b0fa72c commit 85d6931
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 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,21 @@ class TModules(DB.Model):
__tablename__ = "t_modules"
__table_args__ = {"schema": "gn_commons"}

class base_defaultdict(defaultdict):
"""
Avoid polymorphic error when polymorphic identities are declared
in database but absent from venv: fallback on base identity.
Taken from CTFd.
"""

def __missing__(self, key):
return self["base"]

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

1 comment on commit 85d6931

@jacquesfize
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Work done by @bouttier on PR #2792

Please sign in to comment.