[sqlalchemy] Re: Combining aliases with labels

2010-09-15 Thread Jack Kordas
On Sep 9, 9:53 am, Conor conor.edward.da...@gmail.com wrote:
 On 09/08/2010 01:05 PM, Jack Kordas wrote:



  When I try to use both aliases and labels, the results are not named
  as expected.

  Instead of being able to access the columns as label-name_column-
  name it appears as original-table-name_numeric-sequence_column-
  name

  Thanks,
    Jack

  Sample code follows:

  parent = Table('parent', metadata,
      Column('id', INTEGER(), primary_key=True),
      Column('name', VARCHAR(length=128)),
      Column('first_id', INTEGER(),
          ForeignKey(u'child.id')),
    )

  child = Table('child', metadata,
      Column('id', INTEGER(), primary_key=True),
      Column('name', VARCHAR(length=128))
    )

  def test_labels1(conn):
      s = select([parent,child], use_labels=True)
      s = s.where(parent.c.first_id==child.c.id)
      return conn.execute(s).fetchone()

  def test_alias1(conn):
      firstchild = child.alias()
      s = select([parent,firstchild], use_labels=True)
      s = s.where(parent.c.first_id==firstchild.c.id)
      return conn.execute(s).fetchone()

  conn = engine.connect()

  results = test_labels1(conn)
  print results.parent_name
  print results.child_name

  results = test_alias1(conn)
  print 'alias1 results: '
  print results.parent_name
  #print results.firstchild_name # expected this to work
  print results.child_1_name # this worked instead

 You need to set an explicit name for the alias to prevent SQLAlchemy
 from generating an anonymous name[1]:

 firstchild = child.alias(firstchild)

 -Conor

 [1]http://www.sqlalchemy.org/docs/core/expression_api.html#sqlalchemy.sq...

Thanks, that did the trick.  I didn't appreciate the difference
between using the aliased variable in the from clause and generating
names for the selected columns.

-- 
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] Combining aliases with labels

2010-09-08 Thread Jack Kordas
When I try to use both aliases and labels, the results are not named
as expected.

Instead of being able to access the columns as label-name_column-
name it appears as original-table-name_numeric-sequence_column-
name

Thanks,
  Jack

Sample code follows:

parent = Table('parent', metadata,
Column('id', INTEGER(), primary_key=True),
Column('name', VARCHAR(length=128)),
Column('first_id', INTEGER(),
ForeignKey(u'child.id')),
  )

child = Table('child', metadata,
Column('id', INTEGER(), primary_key=True),
Column('name', VARCHAR(length=128))
  )

def test_labels1(conn):
s = select([parent,child], use_labels=True)
s = s.where(parent.c.first_id==child.c.id)
return conn.execute(s).fetchone()

def test_alias1(conn):
firstchild = child.alias()
s = select([parent,firstchild], use_labels=True)
s = s.where(parent.c.first_id==firstchild.c.id)
return conn.execute(s).fetchone()

conn = engine.connect()

results = test_labels1(conn)
print results.parent_name
print results.child_name

results = test_alias1(conn)
print 'alias1 results: '
print results.parent_name
#print results.firstchild_name # expected this to work
print results.child_1_name # this worked instead

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