Multiple mappers found by get_mapper - model inheritance

64 views
Skip to first unread message

Piotr

unread,
May 26, 2021, 9:18:09 AM5/26/21
to sqlalchemy
I'm updating "sqlalchemy_django_query" for SQLAlchemy 1.4 and aside of that I found a peculiar problem - when I call get_mapper on  a table (AnnotatedTable) I get a ValueError due to multiple mappers found.

The model is inherited by two other models and get_mapper finds mappers for all 3 models due to this:


class Conversation(db.Base):
    __tablename__ = 'conversation'
    id = sqlalchemy.Column(postgres_dialect.UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    ...
    type = sqlalchemy.Column(sqlalchemy.Text, nullable=False)

    __mapper_args__ = {
        'polymorphic_identity': 'conversation',
        'polymorphic_on': type,
    }


...


class ContactFormConversation(Conversation):
    __tablename__ = 'contact_form_conversation'
    id = sqlalchemy.Column(sqlalchemy.ForeignKey('conversation.id'), primary_key=True, nullable=False)
    ...

    __mapper_args__ = {
        'polymorphic_identity': 'contact_form_conversation',
    }


...


class RatingConversation(Conversation):
    __tablename__ = 'rating_conversation'
    id = sqlalchemy.Column(sqlalchemy.ForeignKey('conversation.id'), primary_key=True, nullable=False)
    ...

    __mapper_args__ = {
        'polymorphic_identity': 'rating_conversation',
    }


Should the model structure be done differently?

Mike Bayer

unread,
May 26, 2021, 9:21:06 AM5/26/21
to noreply-spamdigest via sqlalchemy
I would need to see what get_mapper() is doing.   the best way to associate the relevant table to a mapper is to look at inspect(Class).mapper.local_table.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
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.

piotr maliński

unread,
May 26, 2021, 9:35:56 AM5/26/21
to sqlal...@googlegroups.com
AnnotatedTable doesn't have a mapper so I call:

get_mapper(annotated_table_object_here)

To then map a column name to a column on a model with _entity_descriptor. In get_mapper this seems to hit the "if isinstance(mixed, sa.Table):" branch and:

if mixed in mapper.tables

ends true to all 3 of them as it's either the parent or child that inherits it.




You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/cH8L4MTVWIY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/d953ab16-fa2b-4792-aa9e-43ee4677713c%40www.fastmail.com.

Mike Bayer

unread,
May 26, 2021, 11:47:09 AM5/26/21
to noreply-spamdigest via sqlalchemy
get_mapper() is not part of SQLAlchemy where is it?

piotr maliński

unread,
May 26, 2021, 11:49:37 AM5/26/21
to sqlal...@googlegroups.com

Mike Bayer

unread,
May 26, 2021, 12:16:13 PM5/26/21
to noreply-spamdigest via sqlalchemy
OK then please report this as a bug in sqlalchemy-utils

right now they are not looking at local table


code can be like:

        mappers = [
            mapper for mapper in all_mappers
            if mixed in {mapper.local_table}
        ]


where above, using "table in set()" makes sure this compared on hash/eq rather than "is" to accommodate for Table vs. AnnotatedTable

piotr maliński

unread,
May 27, 2021, 4:56:35 AM5/27/21
to sqlal...@googlegroups.com
That seems to help. I'll make an issue for it.

Reply all
Reply to author
Forward
0 new messages