On Mar 16, 2011, at 8:12 PM, zaza witch wrote:

> Hi everyone,
> 
> I am currently in work experience ...
> 
> The first step of my work was to build a small application using
> sqlalchemy.
> 
> Association proxy seems to allow to simplify many-to-many
> relationships management.
> Can i use it to store define a class with an attribute which is a list
> of string (or integers ...) ?

I recently put up an example of this which deals with lists of dicts of 
integers, it can be adapted to do lists of strings:

http://groups.google.com/group/sqlalchemy/msg/342a8c4d814d520a





> Let me explain:
> 
> With association proxy, i can do something like:
> 
> class Service(Base):
>    __tablename__ = 'service'
>    id  = Column('s_id', Integer, primary_key=True)
>    name = Column('s_name', String(63), unique=True)
> class NodeService(Base):
>    __tablename__ = 'node_service'
>    node_id  = Column('n_id', Integer, ForeignKey('node.n_id'),
> primary_key=True)
>    service_id  = Column('s_id', Integer, ForeignKey('service.s_id'),
> primary_key=True)
>    service = relationship(Service lazy='joined')
> class Node(Base):
>    __tablename__ = 'node'
>    id  = Column('n_id', Integer, primary_key=True)
>    node_services = relationship(NodeService, cascade="all, delete-
> orphan", lazy='joined')
>    services = AssociationProxy('nodes_services', 'service', lambda
> service: NodeService(service=service))
> ...
> mynode = Node()
> mynode.services.append(Service("myservice"))
> # instead of
> mynode.node_services.append(NodeService(service=Service("myservice")
> session.add(mynode)
> 
> 
> Is there a way to suppress the Service class in order to handle a list
> of string (from python point of view) and have something like:
> 
> class NodeAttribute(Base):
>    __tablename__ = 'node_attribute'
>    node_id  = Column('n_id', Integer, ForeignKey('node.n_id'),
> primary_key=True)
>    attribute  = Column('na_id', String, primary_key=True)
> class Node(Base):
>    __tablename__ = 'node'
>    id  = Column('n_id', Integer, primary_key=True)
>    node_services = relationship(NodeService, cascade="all, delete-
> orphan", lazy='joined')
>    node_attributes = relationship(NodeAttribute cascade="all, delete-
> orphan", lazy='joined')
>    services = AssociationProxy('nodes_services', 'service', lambda
> service: NodeService(service=service))
>    attributes = AssociationProxyLike('node_attributes', ?, ?)
> 
> mynode = Node()
> mynode.attributes.append("myattribute")
> # instead of
> mynode.node_attributes.append(NodeAttribute("myattribute"))
> # attributes look like a list of string (with append/del functions)
> session.add(mynode)
> 
> 
> 
> The second step is to replace sqlalchemy+database by something
> else ... i will make an other message on the topic
> 
> -- 
> 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