Error relating tables in Sqlalchemy

Asked

Viewed 32 times

0

I have two classes mapped: User and Contact with one-to-many relationship (User.).

from sqlalchemy import Column, ForeignKey, Integer, String, UniqueConstraint
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine =create_engine('postgresql+psycopg2://xxxxx:xxxxxxx@localhost/xxxxx', echo=True)
Session = sessionmaker(engine)
session = Session()


Base = declarative_base()

#Tabela Usuario
class Usuario(Base): 
    __tablename__ = 'usuario'

    id = Column(Integer, primary_key = True)
    usuario_nome =  Column (String(100), nullable = True)
    usuario_login = Column(String(50), unique = True, nullable = True)
    usuario_senha = Column(String(20), nullable = True)

    contato = relationship("Contato", back_populates= 'usuario', cascade='all, delete, delete-orphan') 

# Tabela Conatato
class Contato(Base):
    __tablename__ = 'contato'

    id = Column(Integer, primary_key = True)
    contato_nome = Column(String(100), nullable = True)
    contato_nasc = Column(String(10), nullable = True) 
    contato_email = Column(String(50), nullable = True)

    usuario_id = Column(Integer, ForeignKey('usuario.id')) 


    relacao_usuario = relationship("Usuario", back_populates= 'contato') 
    
    Base.metadata.create_all(engine)
    
u1 = Usuario(usuario_nome='delas', usuario_login='delas', usuario_senha='123')
u1.add(u1)
session.commit()

This code gives me a mistake I’m not understanding:

Traceback (most recent call last):
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", line 2060, in get_property
    return self._props[key]
KeyError: 'usuario'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/joao/Documentos/TCC/agenda-contatos/protótipos/src8/model/models.py", line 58, in <module>
    u = connection.session.query(Usuario).get(2)
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1585, in query
    return self._query_cls(entities, self, **kwargs)
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 197, in __init__
    self._set_entities(entities)
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 225, in _set_entities
    self._set_entity_selectables(self._entities)
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 256, in _set_entity_selectables
    ent.setup_entity(*d[entity])
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 4324, in setup_entity
    self._with_polymorphic = ext_info.with_polymorphic_mappers
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 884, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", line 2155, in _with_polymorphic_mappers
    configure_mappers()
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", line 3299, in configure_mappers
    mapper._post_configure_properties()
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", line 1965, in _post_configure_properties
    prop.init()
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/interfaces.py", line 197, in init
    self.do_init()
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py", line 2080, in do_init
    self._generate_backref()
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py", line 2385, in _generate_backref
    self._add_reverse_property(self.back_populates)
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py", line 2001, in _add_reverse_property
    other = self.mapper.get_property(key, _configure_mappers=False)
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", line 2062, in get_property
    util.raise_(
  File "/home/joao/.local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
sqlalchemy.exc.InvalidRequestError: Mapper 'mapped class Contato->contato' has no property 'usuario'

The construction of the table occurs normally, without an error, but when I try to insert something, the error occurs, why?

  • 1

    'Mapped class Contato->contato' has no Property 'usuario' to what all indicates this happens pq Voce did this back_populates= 'usuario' and in the contact put the name as relacao_usuario

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.