Skip to content

Commit

Permalink
feat(api): add association table with alembic
Browse files Browse the repository at this point in the history
Add model in backend and alembic migration

Reviewed-by: andriac
[Refs ticket]: #3
  • Loading branch information
andriacap committed Dec 19, 2022
1 parent 40f9fff commit 296dc14
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""create_cor_module_category
Revision ID: a54bafb13ce8
Revises:
Create Date: 2022-12-06 16:18:24.512562
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "a54bafb13ce8"
down_revision = "b53bafb13ce8"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"
referent_schema = "gn_commons"


def upgrade():
op.create_table(
"cor_module_categorie",
sa.Column(
"id_categorie",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.bib_categorie_site.id_categorie",
name="fk_cor_module_categorie_id_categorie",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.Column("id_module", sa.Integer(),sa.ForeignKey(
f"{referent_schema}.t_modules.id_module",
name="fk_cor_module_categorie_id_module",
ondelete="CASCADE",
onupdate="CASCADE",
), nullable=False),
sa.PrimaryKeyConstraint("id_categorie", "id_module", name="pk_cor_module_categorie"),
schema=monitorings_schema,
)


def downgrade():
op.drop_table("cor_module_categorie", schema=monitorings_schema)
22 changes: 21 additions & 1 deletion backend/gn_module_monitoring/monitoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
from pypnusershub.db.models import User
from geonature.core.gn_monitoring.models import corVisitObserver

cor_module_categorie = DB.Table(
"cor_module_categorie",
DB.Column(
"id_module",
DB.Integer,
DB.ForeignKey("gn_commons.t_modules.id_module"),
primary_key=True,
),
DB.Column(
"id_categorie",
DB.Integer,
DB.ForeignKey("gn_monitoring.bib_categorie_site.id_categorie"),
primary_key=True,
), schema="gn_monitoring")

@serializable
class BibCategorieSite(DB.Model):
Expand All @@ -28,7 +42,7 @@ class BibCategorieSite(DB.Model):
id_categorie = DB.Column(DB.Integer, primary_key=True, nullable=False, unique=True)
label = DB.Column(DB.String, nullable=False)
config = DB.Column(JSONB)


@serializable
class TMonitoringObservationDetails(DB.Model):
Expand Down Expand Up @@ -310,6 +324,12 @@ class TMonitoringModules(TModules):
lazy="joined",
)

categories = DB.relationship(
"BibCategorieSite",
secondary=cor_module_categorie,
lazy="joined"
)


data = DB.Column(JSONB)

Expand Down

0 comments on commit 296dc14

Please sign in to comment.