On Feb 20, 2011, at 10:30 AM, farcat wrote:

> Thank you,
> Basically the multiple inheritance structure is a directional non-
> cyclic graph (i looked at the graph example code in the distribution,
> it uses methods to get next/previous nodes, which could serve as a
> workaround, but seems inelegant) . Members are basically another name
> for attributes.
> I understand the need for the primaryjoin now. However the many to
> many adjacency combination (graph) keeps eluding me. I would like to
> use the association class so i can use what i learned for the members
> class (types have multiple members, members have one type). Some more
> questions:
> 1) Is associationproxy the only way to create an attribute that skips
> the inheritance table, where do I indicate the primaryjoin then?

associationproxy is mostly a Python attribute trick.   It doesn't know very 
much about relationships or primaryjoins, and its only purpose is as a typing 
saver; it takes an expression like Parent.associations[0].child and converts it 
to Parent.children[0].     If you're trying to understand linkages and how they 
relate to the database, it's best to not use associationproxy at first, until 
you understand the directly relational version of things first.    The 
associationproxies are then added to remove the need for explicit "hops" like 
that of Parent.associations[0] in source code.


> 2) Is the point of a relationship() to make items in one class/table
> accessible through an attribute in another class?

relationship() models a foreign key relationship between two tables in terms of 
an object model, providing an Python attribute representing a linkage to the 
remote table.  It has table-level behavior when accessed as a class property in 
that it represents a join condition between the two tables, and row-level 
behavior when accessed as an instance property in that returns a collection or 
scalar attribute populated with the object-level representations of rows 
related to the source object's row representation.

> 3) I am confused about the error I am getting in:
> 
> Base = declarative_base()
> 
> def _create_inheritance(supertype, subtype):
>    return Inheritance(supertype, subtype)
> 
> 
> class Inheritance(Base):
>    __tablename__ = 'Inheritance'
>    sub_name = Column(String(50), ForeignKey('Types.name'),
> primary_key=True)
>    super_name = Column(String(50), ForeignKey('Types.name'),
> primary_key=True)
>    def __init__(self, supertype, subtype):
>        self.supertype = supertype
>        self.subtype = subtype
> 
> class Types(Base):
>    __tablename__ = 'Types'
>    name = Column(String(50), primary_key=True)
>    abstract = Column(Boolean)
>    subtypes = association_proxy('Inheritance', 'subtypes', creator
> =_create_inheritance)
>    supertypes = association_proxy('Inheritance', 'supertypes',
> creator = _create_inheritance)
>    def __init__(self, name, abstract = False):
>        self.name = name
>        self.abstract = abstract
>    def __repr__(self):
>        return "Type(%r)" % (self.name)
> 
> 
>    (self, key))
> sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|Types|Types' has no
> property 'Inheritance'
> 

the arguments to association_proxy() should be the string names of mapped 
attributes, where "mapped attribute" above would be any of the names linked to 
a Column() or relationship() (typically both arguments, and at least the first, 
refers a relationship() and not a Column()).

If you're using the model in the directed_graph.py example, you'd need to set 
up the relationships() in a similar fashion.   The Edge class has two foreign 
keys, which means it has two relationships emanating from it.  Each of those 
relationships in turn specifies a "backref", which means there is an additional 
relationship() each placed on the target, for a total of four relationships.   

> 
> Actually I am getting confused in general about how to implement the
> combination of many to many relationships on the same table. Please
> help.

SQLAlchemy distinguishes between the "association object" pattern which is what 
we have above and "many-to-many", which from a database foreign key perspective 
is identical, but in terms of an object model, association object applies a 
class to the "middle" table whereas "many-to-many" does not.

An example of many-to-many from a class to itself without an explicit mapping 
for the "middle" table would look like the answer at 
http://stackoverflow.com/questions/3149436/sqlalchemy-recursive-many-to-many-relation/3155483#3155483
 .     

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

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

Reply via email to