python - What the heck do I get when I subclass `declarative_base` using SqlAlchemy? -


i have simple sqlalchemy application:

import sqlalchemy sa import sqlalchemy.ext.declarative dec import sqlalchemy.engine.url saurl import sqlalchemy.orm saorm import sqlalchemy.schema sch import abc  class itemtable():     __tablename__ = 'book_items'      @abc.abstractmethod     def _source_key(self):         pass      rowid    = sa.column(sa.integer, sa.sequence('book_page_id_seq'), primary_key=true)     src      = sa.column(sa.string,  nullable=false, index=true, default=_source_key)     dlstate  = sa.column(sa.integer, nullable=false, index=true, default=0)     url      = sa.column(sa.string,  nullable=false, unique=true, index=true)     # [...snip...]  base = dec.declarative_base(cls=itemtable)  class testitem(base):     _source_key = 'test'      def __init__(self, *args, **kwds):         # set default value of `src`. somehow, despite fact `self.src` being set         # string, still works.         self.src = self._source_key          print(self)         print(type(self))         print(super())         print("isinstance of itemtable", isinstance(self, itemtable))         print("isinstance of table", isinstance(self, sch.table))         super().__init__(*args, **kwds)   def test():     test = testitem()   if __name__ == "__main__":      test() 

the idea table schema defined in itemtable, , member attributes defined abstract. ensures child-classes define member attributes, used value defaults instantiated child-class via __init__() hijinks.

anyways, works.

the issue i'm having cannot life of me figure out hell parents of testitem(base) are. know inherits itemtable(), intermediate inheritance of dec.declarative_base(cls=itemtable) inserting whole bunch of methods , "stuff" testitem(base), , don't know there, or it's coming from.

i'm pretty sure there functions make life lot easier regard modifying row in table, since don't know testitem(base) actually inheriting from, have no idea @ in sqlalchemy documentation.

the documentation does declarative_base():

the new base class given metaclass produces appropriate table objects , makes appropriate mapper() calls based on information provided declaratively in class , subclasses of class.

which makes me think possibly testitem(base) child-class of table, isinstance(self, sch.table) returns false, either it's not, or metaclass muckery breaking isinstance.

also, testitem(base) being child-class of table wouldn't make sense logically, because instances of testitem(base) returned when query, each instance representing row.

anyways, i'm thoroughly confused.


update:

@veedrac in comments pointed out classname.mro() gives full inheritance. in case:
testitem.mro() ->
[<class '__main__.testitem'>, <class 'sqlalchemy.ext.declarative.api.base'>, <class '__main__.itemtable'>, <class 'object'>]

the fun thing here there 0 instances of sqlalchemy.ext.declarative.api.base anywhere in sqlalchemy documentation.

the thing documented along sqlalchemy.ext.declarative.api path @ _declarative_constructor, , there ~2 unhelpful sentences there.

well, end solution issues here flat-out dump sqlalchemy entirely.

i know how achieve want using sql. assumed sqlalchemy make things easier, lead inheritance nightmares , lots of bizarre issues.

i'm sure there a way manage want in sqlalchemy, documentation terrible i'm unable find it.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -