[sqlalchemy] Change __tablename__ at runtime?

2010-07-19 Thread Wade Leftwich
I've been using Sqlalchemy to help migrate a bunch of websites into
and out of Drupal. Since a Drupal 'node' can involve fields from 10 or
12 tables, the Declarative approach has been a real timesaver.

But now they're thrown me a serious curveball. It turns out that
Drupal has a 'multisite' mode, where instead of one `node` table you
have `site1_node`, `site2_node`, etc.

I'm not going to try to do a union of `site1_node` and `site2_node` or
anything like that, but -- given that they have exactly the same
structure, is there any way I can define a Node class and specify the
__tablename__ during runtime?

Any advice appreciated; I'm prepared for the advice to be Don't go
there.


Wade Leftwich
Ithaca, NY

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



[sqlalchemy] Re: M:N self-reference

2009-11-09 Thread Wade Leftwich

Something exactly like that. Thanks much.

On Nov 8, 11:28 pm, Mike Conley mconl...@gmail.com wrote:
 Something like this? The association table is declared in the relationships,
 but never referenced when creating or accessing objects.

 class Assoc(Base):
     __tablename__ = 'assoc'
     parent = Column(Integer, ForeignKey('m_to_n.id'), primary_key=True)
     child = Column(Integer, ForeignKey('m_to_n.id'), primary_key=True)

 class MToN(Base):
     __tablename__ = 'm_to_n'
     id = Column(Integer, primary_key=True)
     name = Column(String)
     children = relation('MToN', secondary=Assoc.__table__,
             primaryjoin='MToN.id==Assoc.parent',
             secondaryjoin='MToN.id==Assoc.child',
             backref=backref('parents')
         )
     def __repr__(self):
         return M:N %s %s % (self.id, self.name)

 metadata.create_all()
 compile_mappers()

 p1 = MToN(name='P1')
 p2 = MToN(name='P2')
 p3 = MToN(name='P3')
 c1 = MToN(name='C1')
 c1a = MToN(name='C1A')
 c2 = MToN(name='C2')
 c3 = MToN(name='C3')
 p1.children.append(c1)
 p1.children.append(c1a)
 c1.children.append(c2)
 p2.children.append(c1)
 c3.parents.append(p1)
 c3.parents.append(p3)
 session.add_all([p1, p2, p3])

 session.commit()

 engine.echo=False
 qry_p = session.query(MToN).filter(MToN.name.like('P%'))
 for p in qry_p:
     print '=='
     print p
     for ch1 in p.children:
         print '  ', ch1
         for ch2 in ch1.children:
             print '     ',ch2
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] M:N self-reference

2009-11-05 Thread Wade Leftwich

Hi,

My question is: Is it possible to have a many-to-many relation on a
self reference, without using an Association Object?

I'm using sqlalchemy to work with Drupal tables for a data migration
project. Drupal's taxonomy features hierarchical vocabularies, where
terms live in a term_data table and are associated with other terms
via term_hierarchy. A child can have many parents.

I've gotten the object mapping to basically work, like this:

class TermHierarchy(Base):
__tablename__ = 'term_hierarchy'
childid = Column(u'childid', Integer(), primary_key=True,
nullable=False)
parentid = Column(u'parentid', Integer(), primary_key=True,
nullable=False)

class TermData(Base):
__tablename__ = 'term_data'
id = Column(u'id', Integer(), primary_key=True, nullable=False,
autoincrement=True)
name = Column(u'name', String(length=255))

children_assoc = relation(TermHierarchy,
  primaryjoin=(TermHierarchy.parentid==id),
  foreign_keys=[TermHierarchy.parentid],
  backref=backref('parent', uselist=False),
  uselist=True)

parents_assoc = relation(TermHierarchy,
  primaryjoin=(TermHierarchy.childid==id),
  foreign_keys=[TermHierarchy.childid],
  backref=backref('child', uselist=False),
  uselist=True)

This works OK, except that to get to a parent or child I have to go
through the Association object:

 first = TermData('first')
 second = TermData('second', parent=first)
 third = TermData('third', parent=second)
 another_third = TermData('another_third', parent=second)
 second.children_assoc
[selfref.TermHierarchy object at 0xa69c40c, selfref.TermHierarchy
object at 0xa69c5ac]
 [x.child for x in second.children_assoc]
[selfref.TermData object at 0xa69c48c, selfref.TermData object at
0xa69c56c]


Since there are only 2 columns in the association table, i'd like to
leave it out of the mapping, which would simplify
running queries. I tried several variations on M:N mapping but
couldn't make it work. Before I bang my head against this wall any
more, maybe someone could tell me if it's possible?

Any advice appreciated.

Wade Leftwich
Ithaca, NY

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---