Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

510
Views
FastApi/Pydantic access many to one relationship from parent table

I have a structure like this:

SqlAlchemy models

class MPrueba(Base):

    __tablename__ = 'M_pruebas'

    idpruebas = Column(Integer, primary_key=True)
    idfuentes = Column(ForeignKey('M_fuentes.idfuentes', ondelete='RESTRICT', onupdate='RESTRICT'), index=True)

    M_fuente = relationship('MFuente')


class MRegla(Base):

    __tablename__ = 'M_reglas'

    idreglas = Column(Integer, primary_key=True)
    idpruebas = Column(ForeignKey('M_pruebas.idpruebas', ondelete='RESTRICT', onupdate='RESTRICT'), index=True)
    nombre = Column(String(40))

    M_prueba = relationship('MPrueba') 

As you can see there is a relationship on MRegla class that points to MPrueba class. This mean that when I make some get request on MRegla class, M_prueba field should contain data from MPrueba class. How can I access that relationship from the MPrueba Class ??. I want to generate a pydantic model like this:

pydantic schema for MPrueba class

class Prueba(BaseModel): 
    idpruebas: int
    idfuentes: int
    reglas : # Append the MRegla here

    class Config:
        orm_mode = True

Thanks for your help.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I'm dealing with a similar problem, if I'm understanding correctly. Though I'm not sure that you have a many to one relationship, but regardless.

Use your previously defined Pydantic model:

class MRegla(BaseModel):
    ...

class Prueba(BaseModel): 
    idpruebas: int
    idfuentes: int
    reglas : MRegla

    class Config:
        orm_mode = True

If you need a subset of the MRegla model define an additional Pydantic model with the required fields and use that instead.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!