python - Flask/Sqlaclemy: How to do one to many polymorphic inheritance -
i attempting create 1 many polymorphic relationship in flask/sqlalchemy. concept provide temporary data using authorizations table. table have 2 fields, authorizable_id , authorizable_type. authorizable_id refers id of entry referring to. not foreign key can refer different tables. authorizable_type determines table referring to. example, file, 'file'.
i've seen how similar sqlalchemy documenation pretty implicit role of fields. in addition, require table being referred (here file), have id foreign key refers table holds relationship (discriminator/id). doing opposite, polymorphic relationship holds id of asset. hoping sqlalchemy had value such "polymorphic_field" can indicate field name. i've added code below. guidance appreciated. thank you!
class authorizableinterface(model): __mapper_args__ = { 'polymorphic_identity':'authorizable', } class authorization(model): __tablename__ = 'authorizations' token = column(string(254), nullable=false) ... authorizable_id = column(integer(), foreignkey(authorizableinterface.id)) authorizable_type = column(string(254), nullable=false) __mapper_args__ = { 'polymorphic_on': authorizable_type, 'polymorphic_field' 'authorizable_id' // not real thing, want } class file(authorizableinterface): __tablename__ = 'files' url = column(text(1024), nullable=false) provider_id = column(integer(), foreignkey('file_providers.id')) __mapper_args__ = { 'polymorphic_identity':'file' } class anotherauthorizablething(authorizableinterface): ... __mapper_args__ = { 'polymorphic_identity':'thing' }
Comments
Post a Comment