Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

506
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda