Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Db2 with SQLAlchemy, how to specify default schema

I'm trying to map an existing DB2 database to new python ORM objects. I wrote a very simple mapper class:

class Storage(Base):

    __tablename__ = 'T_RES_STORAGE_SUBSYSTEM'        

    id = Column(Integer,primary_key=True,name='SUBSYSTEM_ID')
    name = Column(String(255),name='NAME')
    namealias = Column(String(256),name='NAME_ALIAS')

But when I try to map it, by executing a query it puts the DB2ADMIN.tablename in front of every query, which of course lead to errors. If I execute the query manually by prepending TPC.tablename to it, then everything works without issues.

How can I specify in a table definition which schema to use?

like image 317
Martino Dino Avatar asked Apr 25 '26 07:04

Martino Dino


1 Answers

Ok so after the help of mustaccio, I found out that in the table_args you have to add schema:

class Storage(Base):

    __tablename__ = 'T_RES_STORAGE_SUBSYSTEM'
    __table_args__ = {'schema' : 'TPC'}

    id = Column(Integer,primary_key=True,name='SUBSYSTEM_ID')
    name = Column(String(255),name='NAME')
    namealias = Column(String(256),name='NAME_ALIAS')
like image 100
Martino Dino Avatar answered Apr 27 '26 22:04

Martino Dino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!