Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid sqla polymorphic errors #2792

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
__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"]

Check warning on line 89 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#L89

Added line #L89 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