B
B
beduin012018-03-05 18:07:35
Python
beduin01, 2018-03-05 18:07:35

How to return an instance of a class from the database?

I don't quite understand the moment. How does an ORM query turn into a class:

class User(Base):
  __tablename__ = 'users'
  id = Column(Integer, primary_key=True)
  name = Column(String)
  adr = relationship('Address', backref='uuu')

class Address(Base):
  __tablename__ = 'addresses'
  id = Column(Integer, primary_key=True)
  email = Column(String, nullable=False)
  # user_id = Column(Integer)
  user_id = Column(Integer, ForeignKey('users.id'))

Base.metadata.create_all(engine)
answer = sess.query(User.id, User.name).first() # вот этот момент

What do we return in answer here? ResultSet or class?
What happens if we write like this:
answer = sess.query(User.id, Address).first() # тогда что вернется?

When is the class returned?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question