Having multiple polymorphic_identity values to map to the same class

216 views
Skip to first unread message

Burak Arslan

unread,
Mar 15, 2018, 4:37:59 AM3/15/18
to sqlal...@googlegroups.com
Hello,

I want two separate polymorphic identity values to map to the same
class. It's OK when saving only the primary one is used.

I have something along the lines of the following:

class Root(TableModel):
    some_field = ...
    __mapper_args__ = {
        'polymorphic_on': 'some_col',
        'polymorphic_identity': 'root',

    }


class Child(Root):
    __mapper_args__ = {
        'polymorphic_identity': 'son',
    }

    some_new_fields = ...

mapper(Child, Root.__table__,
    polymorphic_identity='daughter',
    non_primary=True,
)

However, this fails with

AssertionError: No such polymorphic_identity u'daughter' is defined

For what it's worth, this is sqlalchemy 1.0.17 on python 2

Any tips regarding this? Any piece of documentation that I missed? It'd
be nice if we could just do         'polymorphic_identity': ('son',
'daughter'), or maybe 'polymorphic_secondary_identities': ('daugther',
'some other gende', ) etc.

Best regards,
Burak


Mike Bayer

unread,
Mar 15, 2018, 9:06:29 AM3/15/18
to sqlal...@googlegroups.com
On Thu, Mar 15, 2018 at 4:37 AM, Burak Arslan
<burak....@arskom.com.tr> wrote:
> Hello,
>
> I want two separate polymorphic identity values to map to the same
> class. It's OK when saving only the primary one is used.
>
> I have something along the lines of the following:
>
> class Root(TableModel):
> some_field = ...
> __mapper_args__ = {
> 'polymorphic_on': 'some_col',
> 'polymorphic_identity': 'root',
>
> }
>
>
> class Child(Root):
> __mapper_args__ = {
> 'polymorphic_identity': 'son',
> }
>
> some_new_fields = ...
>
> mapper(Child, Root.__table__,
> polymorphic_identity='daughter',
> non_primary=True,
> )
>
> However, this fails with
>
> AssertionError: No such polymorphic_identity u'daughter' is defined

"what" exactly fails? just setting up mapper() ?

I don't actually understand what this would be meant to achieve. if
you simply want to load Child objects for both the "daughter" and
"son" identities, polymorphic_on should be made into a case statement,
see the example at
http://docs.sqlalchemy.org/en/latest/orm/mapping_api.html#sqlalchemy.orm.mapper.params.polymorphic_on
:

class Employee(Base):
__tablename__ = 'employee'

id = Column(Integer, primary_key=True)
discriminator = Column(String(50))

__mapper_args__ = {
"polymorphic_on":case([
(discriminator == "EN", "engineer"),
(discriminator == "MA", "manager"),
], else_="employee"),
"polymorphic_identity":"employee"
}





>
> For what it's worth, this is sqlalchemy 1.0.17 on python 2
>
> Any tips regarding this? Any piece of documentation that I missed? It'd
> be nice if we could just do 'polymorphic_identity': ('son',
> 'daughter'), or maybe 'polymorphic_secondary_identities': ('daugther',
> 'some other gende', ) etc.
>
> Best regards,
> Burak
>
>
> --
> 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.
Reply all
Reply to author
Forward
0 new messages