I am having trouble having the query produce results. I know there is 1 value in the table, but I cannot get the data to show in Python.
I am also not getting any errors in the run.
I have the following code.
db = sa.create_engine("/url")
Session = sessionmaker(bind=db)
Base = declarative_base()
class SomeTable(Base):
__tablename__ = 'schema.sometable'
tId = Column(Integer, primary_key=True)
PlanCode = Column(String(50), primary_key=True)
cId = Column(Integer)
lId = Column(Integer)
ModifiedDate = Column(DateTime)
ModifiedChange = Column(String(50))
def main() -> None:
Base.metadata.create_all(db)
with Session() as session:
result = session.query(SomeTable).all()
print(result)
if __name__ == "__main__":
main()
Sql Table schema is :
tId is Int
PlanCode is Nvarchar(50)
cId is Int
lId is Int
ModifiedDate is DateTime
ModifiedChange is Nvarchar(50)
I have tried removing the Primary Key constraints for tId and PlanCode in SQL, adding them back in as both nonclustered and clustered.
I have made many changes in the python code and much of the time it has ran but when calling “count()” it shows “0” or when calling “all()” is shows “[]”
I am unable to get a data to show in python, I am also using Microsoft SQL.
What changes can I make to get the data to show?
David Cresap is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
class SomeTable(Base):
__tablename__ = 'some_schema.sometable'
will not work as you might expect. It will look for the data in a table called [some_schema.sometable] in the default schema for your database connection. I suspect that you want to do
class SomeTable(Base):
__tablename__ = 'sometable'
__table_args__ = {"schema": "some_schema"}
1