On Fri, Oct 20, 2017 at 3:15 PM, Mike Bayer <mike...@zzzcomputing.com> wrote: > On Fri, Oct 20, 2017 at 2:40 PM, Sven <sven.du...@gmail.com> wrote: >> Thank you for your answer. >> >> I'm sorry, I have not been clear enough regarding the "__abstract__ = True". >> >> I suppose that I will have to add this to a lot of classes for the following >> reasons : >> 1) because it will allow me to persist the classes one by one and still be >> able to run and test the project (i will not have all the errors because >> there is no table name and no primary key). So, I would have to delete the >> "__abstract__" when I begin to work on the persistence of a new class. >> 2) because I thought that the best solution in this case is to map only the >> concrete classes. So, in my example, I would have to map "Player" and "NPC", >> but not "Character". So only the classes at the bottom of the hierarchy >> would have to be mapped. That's still a lot of classes but probably easier >> to implement. > > OK, so I see you are looking to add something like "Mappable" or > similar only to those classes that are concrete. > > how about this? > > from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta > from sqlalchemy import Column, Integer > > > class MetaBase(type): > > def __init__(cls, nom, bases, contenu): > type.__init__(cls, nom, bases, contenu) > print("Init MetaBase") > pass > > > class DeclMetaBase(MetaBase, DeclarativeMeta): > > def __init__(cls, nom, bases, contenu): > super(MetaBase, cls).__init__(nom, bases, contenu) > print("Init DeclMetaBase")
actually like this: class DeclMetaBase(MetaBase, DeclarativeMeta): def __init__(cls, nom, bases, contenu): MetaBase.__init__(cls, nom, bases, contenu) DeclarativeMeta.__init__(cls, nom, bases, contenu) print("Init DeclMetaBase") > > Base = declarative_base(metaclass=DeclMetaBase) > > > class Stockable(metaclass=MetaBase): > > def __init__(self): > print("Init Stockable") > > > class Character(Stockable, Base): > > __tablename__ = 'characters' > id = Column(Integer, primary_key=True) > > def __init__(self, name): > self.name = name > print("Init character") > > print(repr(Character.__table__)) > jean = Character("Jean") > print(jean.name) > > > >> >> But I have to say that this is not absolutely clear for me for now. This is >> the first time I use SQLAlchemy. Do you think that this method is possible >> and is the right way to proceed ? > > > > Whether or not it "works" involves mostly the database schema that > gets created and how reasonable this schema is from a relational / > database performance / complexity perspective. This depends a lot on > how these class hierarchies are organized and how they interact with > each other, so it's difficult to say. > > > >> >> Le vendredi 20 octobre 2017 20:02:40 UTC+2, Mike Bayer a écrit : >>> >>> >>> CONTINUING ! sorry >>> >>> >>> On Fri, Oct 20, 2017 at 11:55 AM, Sven Dumay <sven....@gmail.com> wrote: >>>> >>>> >>>> I tried other things and I found the following solution : >>>> >>>> from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta >>>> from sqlalchemy import Column, Integer >>>> >>>> class MetaBase(DeclarativeMeta): >>>> >>>> def __init__(cls, nom, bases, contenu): >>>> super(MetaBase, cls).__init__(nom, bases, contenu) >>>> print("Init MetaBase") >>>> >>>> Base = declarative_base(metaclass = MetaBase) >>>> >>>> class Stockable(Base): >>>> >>>> __abstract__ = True >>>> >>>> def __init__(self): >>>> print("Init Stockable") >>>> >>>> class Character(Stockable): >>>> >>>> __tablename__ = 'characters' >>>> id = Column(Integer, primary_key=True) >>>> >>>> def __init__(self, name): >>>> self.name = name >>>> print("Init character") >>>> >>>> >>>> jean = Character("Jean") >>>> print(jean.name) >>>> >>>> >>> >>> this seems like roughly the correct approach. >>> >>>> >>>> It seems to work. I get the following result : >>>> >>>> >>> >>>> Init MetaBase >>>> Init MetaBase >>>> Init MetaBase >>>> Init compte >>>> Jean >>>> >>> >>>> >>>> However, the problem with this method is that I have to add "__abstract__ >>>> = True" to every class which is inherited by Stockable... so, about 400 >>>> classes. >>> >>> >>> I don't see why that is. If these classes are mapped to tables (which, if >>> they are persisted, they are), then there is no reason to add >>> "__abstract__". As in my previous email, how these 400 classes link >>> to tables is what needs to be answered and then we can formulate the correct >>> calling style. >>> >>> >>>> >>>> It is not very clean. Is it possible to avoid that by using something >>>> similar to my first code ? It would be great ! >>>> >>>> Thank you very much. >>>> >>>> Sven >>>> >>>> -- >>>> SQLAlchemy - >>>> The Python SQL Toolkit and Object Relational Mapper >>>> >>>> http://www.sqlalchemy.org/ >>>> >>>> To post example code, please provide an MCVE: Minimal, Complete, and >>>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full >>>> description. >>>> --- >>>> You received this message because you are subscribed to the Google Groups >>>> "sqlalchemy" group. >>>> To unsubscribe from this group and stop receiving emails from it, send an >>>> email to sqlalchemy+...@googlegroups.com. >>>> To post to this group, send email to sqlal...@googlegroups.com. >>>> Visit this group at https://groups.google.com/group/sqlalchemy. >>>> For more options, visit https://groups.google.com/d/optout. >>> >>> >> -- >> SQLAlchemy - >> The Python SQL Toolkit and Object Relational Mapper >> >> http://www.sqlalchemy.org/ >> >> To post example code, please provide an MCVE: Minimal, Complete, and >> Verifiable Example. See http://stackoverflow.com/help/mcve for a full >> description. >> --- >> You received this message because you are subscribed to the Google Groups >> "sqlalchemy" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to sqlalchemy+unsubscr...@googlegroups.com. >> To post to this group, send email to sqlalchemy@googlegroups.com. >> Visit this group at https://groups.google.com/group/sqlalchemy. >> For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.