1

I am using sqlalchemy.orm and defining my table objects like so (trimmed down for brievety):

class my_table(Base):

    __tablename__ = 'my_data'
    __table_args__ = { 'schema': self.db_schema }


    foo = Column(String, nullable=False)

    @orm.reconstructor
    def __init__(self, schema):
        self.db_schema = schema

I want to be able to pass variables into the creation of this class like the db_schema variable above. The docs seem to imply the @orm.reconstructor decorator is used to do this but I can't figure out how to go about this approach.

My example above complains:

NameError: name 'self' is not defined

This class is defined in a library and the data I want to feed it during creation is pulled from a combination of command line arguments to main() and values pulled from a config file.

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
BenH
  • 690
  • 1
  • 11
  • 23
  • It's not the reconstructor that's complaining. There is no `self` in a class body (your `__table_args__` definition). Also if you'd read the documentation a bit, you'd notice that ["The reconstructor will be invoked with no arguments."](http://docs.sqlalchemy.org/en/latest/orm/constructors.html#sqlalchemy.orm.reconstructor) and so wrapping `__init__` might not be the best solution. All in all there is no need to set `self.db_schema` in your reconstructor. The schema is set in stone when the *class* is constructed. – Ilja Everilä Mar 28 '18 at 07:42
  • Related: https://stackoverflow.com/questions/49409691/flask-sqlalchemy-acess-different-schema – Ilja Everilä Mar 28 '18 at 07:49

0 Answers0