Re: Re[4]: [sqlalchemy] Invertinace mapped type_id to fix value for each child class
I probably wouldn't use this: if test_type == ChildClass1().typ_id: ...simply because creating an instance of the object just to get access to the typ_id seems like a waste of effort. If you really need to check integer typ_id values, the staticmethod approach seems fine. Simon On Mon, Apr 12, 2021 at 8:58 PM 'Sören Textor' via sqlalchemy wrote: > > Hi Simon > Again you really helped me out. I don't know what point I missed, but > now it works. As usual it's not as simpe le or lets say there are a lot > more code pieces to change before I can really test it in my code. But I > got it. > > just one more thing: > I often have to check if a given tpye t the class type. Therefore I > usally use the statci method. > > Thus what would you do: > > if test_type == ChildClass1().typ_id: > or > if test_type==ChildClass.TypID(): > > and to ensure only TypId exists fpr that type: > __mapper_args__ = { > "polymorphic_identity": ChildClass.TypID(), > } > > And as I said: Thanks a lot! > > SirAnn > > > -- Originalnachricht -- > Von: "Simon King" > An: sqlalchemy@googlegroups.com > Gesendet: 12.04.2021 20:26:48 > Betreff: Re: Re[2]: [sqlalchemy] Invertinace mapped type_id to fix value > for each child class > > >Here's a standalone working example: > > > >import sqlalchemy as sa > >import sqlalchemy.orm as saorm > >from sqlalchemy.ext.declarative import declarative_base > > > > > >Base = declarative_base() > > > > > >class Objekt(Base): > > __tablename__ = "objekt" > > id = sa.Column(sa.Integer, primary_key=True) > > typ_id = sa.Column(sa.Integer, sa.ForeignKey("objekt_typ.id")) > > typ = saorm.relationship("ObjektTyp") > > name = sa.Column(sa.String(100)) > > > > __mapper_args__ = { > > "polymorphic_on": typ_id, > > } > > > > > >class ObjektTyp(Base): > > __tablename__ = "objekt_typ" > > id = sa.Column(sa.Integer, primary_key=True) > > name = sa.Column(sa.String(100)) > > > > > >class ChildObjekt1(Objekt): > > __tablename__ = "child_objekt1" > > id = sa.Column(sa.Integer, sa.ForeignKey(Objekt.id), primary_key=True) > > text = sa.Column(sa.String(255)) > > > > __mapper_args__ = { > > "polymorphic_identity": 1, > > } > > > > > >class ChildObjekt2(Objekt): > > __tablename__ = "child_objekt2" > > id = sa.Column(sa.Integer, sa.ForeignKey(Objekt.id), primary_key=True) > > text = sa.Column(sa.String(255)) > > > > __mapper_args__ = { > > "polymorphic_identity": 2, > > } > > > > > >if __name__ == "__main__": > > engine = sa.create_engine("sqlite://") > > Base.metadata.create_all(bind=engine) > > Session = saorm.sessionmaker(bind=engine) > > > > session = Session() > > child1type = ObjektTyp(id=1, name="child1") > > child2type = ObjektTyp(id=2, name="child1") > > > > child1 = ChildObjekt1(text="child 1 text") > > child2 = ChildObjekt2(text="child 2 text") > > > > session.add_all([child1type, child2type, child1, child2]) > > session.flush() > > > > for obj in session.query(Objekt): > > print(obj) > > > > > >Simon > > > >On Mon, Apr 12, 2021 at 6:40 PM 'Sören Textor' via sqlalchemy > > wrote: > >> > >> class Objekt(db.Model): > >> __tablename__ = 'objekt' > >> > >> def __init__(self,**kwargs): > >> super().__init__(**kwargs) > >> > >> id = db.Column(db.Integer, primary_key=True) > >> typ_id = db.Column(db.Integer, db.ForeignKey('objekt_typ.id')) > >> typ= db.relationship("ObjektTyp") > >> name = db.Column(db.String(100)) > >> > >> __mapper_args__ = { > >> 'polymorphic_on': typ_id > >> } > >> > >> class ChildObjekt1(Objekt): > >> __versioned__ = {} > >> __tablename__ = 'child_objekt1' > >> > >> @staticmethod > >> def TypId(): > >> return 7 > >> > >> # User fields > >> def __init__(self,**kwargs): > >> super().__init__(**kwargs) > >> #super().__init__(typ_id=ChildObjekt1.TypId(), **kwargs) > >> > >> ### > >> id db.Column(db.Integer, db.ForeignKey('objekt.id'), > >> primary_key=True) > >> text = db.Column(db.String(255 ), default='') > >> > >> __mapper_args__ = { > >> 'polymorphic_identity': 7, > >> } > >> > >> > >> leads to: > >> venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 1542, in > >> _configure_polymorphic_setter > >> self.polymorphic_on = self._props[self.polymorphic_on] > >> KeyError: 'typ_id' > >> > >> raise exception > >> sqlalchemy.exc.ArgumentError: Can't determine polymorphic_on value > >> 'typ_id' - no attribute is mapped to this name. > >> > >> maybe i do something totally worg.. I am also using sql continuum > >> > >> -- Originalnachricht -- > >> Von: "Simon King" > >> An: sqlalchemy@googlegroups.com > >> Gesendet: 12.04.2021 19:06:11 > >> Betreff: Re: [sqlalchemy] Invertinace mapped type_
Re[4]: [sqlalchemy] Invertinace mapped type_id to fix value for each child class
Hi Simon Again you really helped me out. I don't know what point I missed, but now it works. As usual it's not as simpe le or lets say there are a lot more code pieces to change before I can really test it in my code. But I got it. just one more thing: I often have to check if a given tpye t the class type. Therefore I usally use the statci method. Thus what would you do: if test_type == ChildClass1().typ_id: or if test_type==ChildClass.TypID(): and to ensure only TypId exists fpr that type: __mapper_args__ = { "polymorphic_identity": ChildClass.TypID(), } And as I said: Thanks a lot! SirAnn -- Originalnachricht -- Von: "Simon King" An: sqlalchemy@googlegroups.com Gesendet: 12.04.2021 20:26:48 Betreff: Re: Re[2]: [sqlalchemy] Invertinace mapped type_id to fix value for each child class Here's a standalone working example: import sqlalchemy as sa import sqlalchemy.orm as saorm from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Objekt(Base): __tablename__ = "objekt" id = sa.Column(sa.Integer, primary_key=True) typ_id = sa.Column(sa.Integer, sa.ForeignKey("objekt_typ.id")) typ = saorm.relationship("ObjektTyp") name = sa.Column(sa.String(100)) __mapper_args__ = { "polymorphic_on": typ_id, } class ObjektTyp(Base): __tablename__ = "objekt_typ" id = sa.Column(sa.Integer, primary_key=True) name = sa.Column(sa.String(100)) class ChildObjekt1(Objekt): __tablename__ = "child_objekt1" id = sa.Column(sa.Integer, sa.ForeignKey(Objekt.id), primary_key=True) text = sa.Column(sa.String(255)) __mapper_args__ = { "polymorphic_identity": 1, } class ChildObjekt2(Objekt): __tablename__ = "child_objekt2" id = sa.Column(sa.Integer, sa.ForeignKey(Objekt.id), primary_key=True) text = sa.Column(sa.String(255)) __mapper_args__ = { "polymorphic_identity": 2, } if __name__ == "__main__": engine = sa.create_engine("sqlite://") Base.metadata.create_all(bind=engine) Session = saorm.sessionmaker(bind=engine) session = Session() child1type = ObjektTyp(id=1, name="child1") child2type = ObjektTyp(id=2, name="child1") child1 = ChildObjekt1(text="child 1 text") child2 = ChildObjekt2(text="child 2 text") session.add_all([child1type, child2type, child1, child2]) session.flush() for obj in session.query(Objekt): print(obj) Simon On Mon, Apr 12, 2021 at 6:40 PM 'Sören Textor' via sqlalchemy wrote: class Objekt(db.Model): __tablename__ = 'objekt' def __init__(self,**kwargs): super().__init__(**kwargs) id = db.Column(db.Integer, primary_key=True) typ_id = db.Column(db.Integer, db.ForeignKey('objekt_typ.id')) typ= db.relationship("ObjektTyp") name = db.Column(db.String(100)) __mapper_args__ = { 'polymorphic_on': typ_id } class ChildObjekt1(Objekt): __versioned__ = {} __tablename__ = 'child_objekt1' @staticmethod def TypId(): return 7 # User fields def __init__(self,**kwargs): super().__init__(**kwargs) #super().__init__(typ_id=ChildObjekt1.TypId(), **kwargs) ### id db.Column(db.Integer, db.ForeignKey('objekt.id'), primary_key=True) text = db.Column(db.String(255 ), default='') __mapper_args__ = { 'polymorphic_identity': 7, } leads to: venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 1542, in _configure_polymorphic_setter self.polymorphic_on = self._props[self.polymorphic_on] KeyError: 'typ_id' raise exception sqlalchemy.exc.ArgumentError: Can't determine polymorphic_on value 'typ_id' - no attribute is mapped to this name. maybe i do something totally worg.. I am also using sql continuum -- Originalnachricht -- Von: "Simon King" An: sqlalchemy@googlegroups.com Gesendet: 12.04.2021 19:06:11 Betreff: Re: [sqlalchemy] Invertinace mapped type_id to fix value for each child class >I don't understand this comment: > >> I though on polymorphic_on, but I think that does not work because of the fact that type_id ha a foreign key ... > >As far as I can tell, you ought to have this in the base class: > > __mapper_args__ = { > 'polymorphic_on': typ_id > } > >And this in the subclass: > > __mapper_args__ = { > 'polymorphic_identity': 7, > } > >...and you should get rid of the typ_id function and the >"Objekt.typ_id = ChildClass.typ_id" line. > >Does that work for you? > >Simon > >On Mon, Apr 12, 2021 at 5:18 PM 'Sören Textor' via sqlalchemy > wrote: >> >> I run into a problem and don't know how to solve it. >> The theory is very simple: I habe one base class table with name, id and type column >> The child class shall have a unique type_id (all child_class1 objekt shall get type_id 7, all