I'd discourage using metaclasses as they are more difficult to use in cases 
like these than mixins, which can accomplish the vast majority of use cases.

I can't help with your metaclass case since there is no code here to work with, 
but in general a dynamic __mapper_args__, whether using a mixin or a base 
class, can be done like this:

from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base, declared_attr

Base = declarative_base()

class MyMixin(object):
    @declared_attr
    def __mapper_args__(cls):
        base_id = cls.__bases__[1].id  # or "A.id", or whatever you need to do
                                       # to get to the correct superclass
        this_id = cls.id
        return {"inherit_condition": base_id == this_id}

class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)


class B(MyMixin, A):
    __tablename__ = 'b'

    id = Column(Integer, ForeignKey('a.id'), primary_key=True)



On Jul 3, 2013, at 6:42 AM, Alexey Vihorev <[email protected]> wrote:

> Hi! Got this setup:
>  
>  
> class Document(Base):
>     __tablename__ = 'document'
>     discriminator = Column('type', String(30))
>     __mapper_args__ = {'polymorphic_identity':'document',
>                        'polymorphic_on': discriminator}
>     id = Column(Integer, primary_key=True)
>     number = String(20)
>  
> class Invoice(Document):
>     __tablename__ = 'invoice'
>     __mapper_args__ = {'polymorphic_identity':'invoice'}
>     id = Column(Integer, ForeignKey('document.id'), primary_key=True)
>     customer = String(100)
>     linked_document_id = Column(Integer, ForeignKey('document.id'))
>  
>  
> And, predictably, I get “sqlalchemy.exc.AmbiguousForeignKeysError: Can't 
> determine join between 'document' and 'invoice'; tables have more than one 
> foreign key constraint relationship between them. Please specify the 
> 'onclause' of this join explicitly.” As I understand, augmenting Invoice’s 
> __mapper_args__ with 'inherit_condition ': (id == Document.id) will deal with 
> the problem. However, in my case I cannot call(id == Document.id), because it 
> all is going on inside a metaclass I’ve devised for reasons beyond this topic 
> and the class being created (Invoice) does not yet exist at the time of 
> setting __mapper_args__ , so accessing this ID class member is problematic. 
> So the question is this: can I set 'inherit_condition' by using some other 
> way, like string form or something? Thanks!
>  
> 
> -- 
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to