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 ...

right know I get an error:

c = ChildClass()
db.session.add(c)
db.session.commit()

c.typ -> UnmappedColumnError('No column objekt.typ_id is configured on mapper mapped class ChildClass1->child_class1...')

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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/em9f57039b-c8a0-4dce-93db-9352f992db8a%40textors-01.

Reply via email to