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

How do I defer reflection on a view/table without primary keys?

$
0
0

I have a view xyz.MyTable that I'd like to build a model for. It doesn't have a unique column but the combination of columns col1 and col2 are guaranteed to be unique.

I also don't have access to the engine at declaration, so I need to use deferred reflection. Here is my attempt:

import sqlalchemyimport sqlalchemy.ext.declarativeBase = sqlalchemy.orm.declarative_base()class MyTable(sqlalchemy.ext.declarative.DeferredReflection, Base):    __tablename__ = 'MyTable'    __table__ = sqlalchemy.Table(__tablename__, Base.metadata, schema='xyz', autoload=True)    __mapper_args__ = {'primary_key': [__table__.c.col1, __table__.c.col2]} 

However, this results in the following:

KeyError: 'col1'

This makes sense, because col1 can't be an object until reflection occurs. But how can I declare the primary key column? Note that I have no control over the database and cannot make any changes to the view.


Viewing all articles
Browse latest Browse all 23247

Trending Articles



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