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
<[email protected]> 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 child_class2 objekts type_id = 8, ...)
>
> How can I map the base class typ_id to an hard coded value for eahc class
> type.
> My actual approach does not change the type_id-columns of Objekt and after
> saving the objekt the column Objekt.type_id entry is always empty for all
> entries :-(
>
> 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))
>
> class ChildClass1(Objekt):
> __tablename__ = 'child_class1'
>
> @staticmethod
> def typ_id():
> return 7
>
> def __init__(self,**kwargs):
> super().__init__(**kwargs)
> Objekt.typ_id = ChildClass1.typ_id() ### fix type
>
> id = db.Column(db.Integer, db.ForeignKey('objekt.id'), primary_key=True)
> text = db.Column(db.String(255 ), default='')
>
> __mapper_args__ = {
> 'polymorphic_identity':'child_class1',
> }
>
>
> any ideas where to look? I though on polymorphic_on, but I think that does
> not work because of the fact that type_id ha a foreign key ...
>
> SirAnn
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/ema56ad245-cad9-4096-8c55-9d75e8d52ea2%40textors-01.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexeCQd6%2B5-O%3D1H8J8Zmfrg8vDsPmHjLg4PFY9RTySNdJ3w%40mail.gmail.com.