Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

sqlalchemy returns instance of the class but it does not print correctly

$
0
0

I have code like this:

from sqlalchemy import create_engine, Column, String,and_,insert,select,Integer,deletefrom sqlalchemy.orm import declarative_basefrom sqlalchemy.orm import sessionmaker# Define your modelBase = declarative_base()class MyTable(Base):    __tablename__ = 'my_table'    id = Column(Integer, primary_key=True)    product = Column(String)    market = Column(String)    idea = Column(String)# Create a connection to your databaseengine = create_engine('postgresql+psycopg://postgres:2001@localhost:5432/postgres')# MyTable.__table__.drop(engine)Base.metadata.create_all(engine)# Create a sessionSession = sessionmaker(bind=engine)session = Session()session.execute(    insert(MyTable),    [        {"id":1,"product":"BG","market":"JY","idea":"sad"},        {"id":2,"product":"GF","market":"HN","idea":"happy"},    ]    )session.commit()results = session.scalars(select(MyTable).where(MyTable.id==1)).all()print(results)

Results is :

[<__main__.MyTable object at 0x103818650>]

I was expecting to have the print results as:

[MyTable(id=1,product='BG',market='JY',idea='sad')]

Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>