Correction clés étrangères

This commit is contained in:
2024-06-09 23:53:25 +02:00
parent 828becbd4e
commit 58ffe8a46a
4 changed files with 12 additions and 8 deletions

View File

@ -123,7 +123,7 @@ class ResultatsDepartement(Base):
id: Mapped[int] = mapped_column(primary_key=True)
dpt_id: Mapped[str] = mapped_column(ForeignKey("departement.code_insee"))
resultats_region_id: Mapped[int] = mapped_column(ForeignKey("resultats2024_region.id"))
resultats_region_id: Mapped[int] = mapped_column(ForeignKey("resultats2024_region.id"), nullable=True)
inscrits: Mapped[int] = mapped_column(Integer(), default=0)
votants: Mapped[int] = mapped_column(Integer(), default=0)
abstentions: Mapped[int] = mapped_column(Integer(), default=0)

View File

@ -149,8 +149,12 @@ def importer_resultats_departements(engine: Engine, debug: bool = False) -> None
resultats_departement.nuls = resultats["nuls"]
resultats_departement.exprimes = resultats["exprimes"]
else:
resultats_region = session.execute(select(ResultatsRegion).filter_by(region_id=reg_code)) \
.scalar_one_or_none()
resultats_departement = ResultatsDepartement(
dpt_id=dpt_code,
resultats_region_id=resultats_region.id if resultats_region else None,
inscrits=resultats["inscrits"],
abstentions=resultats["abstentions"],
votants=resultats["votants"],