No such polymorphic_identity is defined

2,731 views
Skip to first unread message

moggie

unread,
Dec 28, 2009, 6:20:05 AM12/28/09
to sqlalchemy
Greetings,

Hope you are well. In my example I have two objects 'HttpTest' and
'SmtpTest' that both inherit from their super-class called 'Test'.

Due to the nature of the relationship, 'Test' will never actually be
stored in the database so I've not mapped that. However, a mixture of
'HttpTest' and 'SmtpTest' objects are both to be stored in the
hosttest_table, thus I created a mapping for them like this:

httptest_mapper = mapper(HttpTest, hosttest_table, \
polymorphic_on=hosttest_table.c.type,
polymorphic_identity='http')
smtptest_mapper = mapper(SmtpTest, hosttest_table, \
polymorphic_on=hosttest_table.c.type,
polymorphic_identity='smtp')

Unfortunately when I do, I get this error message:

No such polymorphic_identity 'smtp' is defined

I was hoping SQLA would understand this and let me pack the two
objects into the same table, but for some reason it doesn't like it.

If someone could please give me some pointers on how to proceed, it
would be greatly appreciated. Thank you in advance for your time and
consideration.

Kind regards,
moggie.

Michael Bayer

unread,
Dec 28, 2009, 10:31:08 AM12/28/09
to sqlal...@googlegroups.com

the pattern you're attempting here is called "single table inheritance".
You will need to define a common base class between your two classes, but
it never need be instantiated directly. Full documentation on how to use
this approach is at
http://www.sqlalchemy.org/docs/05/mappers.html#single-table-inheritance .

>
> Kind regards,
> moggie.
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>
>

mog

unread,
Dec 29, 2009, 12:28:19 PM12/29/09
to sqlal...@googlegroups.com, Michael Bayer


Hi Michael,

Thank you very much for getting back to me. I think I realise now that
to make this work I *must* also map the super-class of the polymorphic
objects and not map just the ones I want to use. This seems to be
working for me now.

Thanks again :)

Reply all
Reply to author
Forward
0 new messages