1

Currently im running into an problem when trying to append a insert to my sqlalchemy session.

Line that is giving me errors:

DB_SESSION.add(TABLE.insert().values(...))

Error:

sqlalchemy.orm.exc.UnmappedInstanceError: Class 'sqlalchemy.sql.dml.Insert' is not mapped

My Table Schema:

TABLE = Table(
    'tablename', metaData,
    Column('value1', NVARCHAR(5), primary_key=True),
    Column('value2', Integer, primary_key=True),
    Column('value3', NVARCHAR(30)),
    Column('value4', NVARCHAR(50)),
    Column('value5', Date)
)

Running select on that Table works fine. For example:

query = TABLE.select().where((...) and (...))
for row in WAREHOUSE_SQL_CONNECTION.execute(query):
    (...)
Kevin Müller
  • 722
  • 6
  • 18
  • 1
    The answers to [this question](https://stackoverflow.com/questions/34322471) might be of some use to you. – SuperShoot Oct 01 '19 at 08:51
  • @SuperShoot After reading through that question, I cannot find anything relating to my problem. I do know the difference between engine and connection executes for example. – Kevin Müller Oct 01 '19 at 09:04
  • 2
    I thought [this answer](https://stackoverflow.com/a/42772654/6560549) was pertinent, particularly WRT describing the session as a construct that is particular to the `ORM`, whereas you are trying to issue a `core` select through it. In other words, `Session.add()` expects an ORM mapped class, not a core sql construct. Any answers in that question give fine examples of how to execute a core sql statement. – SuperShoot Oct 01 '19 at 09:22

1 Answers1

0

You can try:

query = TABLE.select().where((...) and (...))
for row in query.all():
    # Code to update fields of row goes here
    db.session.add(updated_row)
    db.session.commit()
Ishan Joshi
  • 487
  • 3
  • 7