[sqlalchemy] Re: Managing transactions from Session using raw_connections

2010-12-07 Thread Ralph Heinkel
Hi Michael,

thanks for your help.
Since some of my engines are not bound to mappers I've tried the
approach suggested in your previous post to subclass Session so that
'get_bind' would accept an additional 'engine' argument. This works
fine with Session.execute() which passes **kw down to the 'get_bind'
method, but unfortunately Session.connection() doesn't. Is this
omitted intentionally, or could it be added?

Thanks a lot,

Ralph

 Here you'd use Session.execute() and Session.connection() to get at the 
 Connection you'd normally get from engine.contextual_connect() 
 (http://www.sqlalchemy.org/docs/orm/session.html#using-sql-expressions...) .  
   execute() and connection() accept a mapper argument for the case where 
 individual engines are associated with individual mappers, and both 
 ultimately call get_bind() which you can override via subclass if desired to 
 accept other kinds of arguments.


-- 
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: Trouble detaching all objects with relationships intact

2010-12-07 Thread Ian Thompson
On Dec 6, 5:53 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Dec 6, 2010, at 11:52 AM, Ian Thompson wrote:





  On Dec 6, 4:49 pm, Ian Thompson quorn...@gmail.com wrote:
  I've generated a test data set and then want to have all of the
  created objects and relationships available for reference easily.
  To avoid possible pollution of this fixture I want to detach the
  objects from the sessions to avoid them getting any updates applied.

  To do this I am attempting to load all data via joinedload and then
  calling session.expunge_all:

      print assets['test_asset_1'].attributes # OK

  session.query(Asset).options(joinedload(Asset.asset_attributes)).populate_e
   xisting().all()
      session.expunge_all()
      print assets['test_asset_1'].attributes # ERROR

  None of the relationships on the objects survive the expunge.

      sqlalchemy.orm.exc.DetachedInstanceError:
      Parent instance Asset at ... is not bound to a Session;
      lazy load operation of attribute 'asset_attributes' cannot proceed
  (cont...)
  Is there a correct way to detach all current data from the Session
  fully loading any relationships? (Small data set so memory is not a
  concern.)

 This use case seems like it would be more easily solved using a new Session 
 just for that load.   To work with the objects detached, all deferred 
 attributes and lazy-loading relationship attributes which you will need to 
 access would need to either be eagerly loaded (there's now three ways to 
 eagerly load relationship() attributes), or otherwise accessed via 
 obj.attribute before the objects are expunged.

Hi Michael, thanks for your reply.

I had thought by using joinedload (or eagerload) and populate_existing
I would be doing an eager load of the specified relation.

Also, in my example I do access the attributes relationship (with a
print), then after the expunge the same fails. Is there a way I can
ensure the data is preserved after expunging?

Thanks
Ian

-- 
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: Problem with one relation

2010-12-07 Thread Alvaro Reinoso
The relation is OK, one-to-many between Playlist and PlaylisItem.
However, PlaylistItem can contain one Playlist or one Layout and that
Playlist could be in many PlaylistItems. I know it's weird relation if
I already have Playlist as parent of PlaylistItem, but could I get
this?

Thanks!

On Dec 6, 3:51 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Dec 6, 2010, at 2:39 PM, Alvaro Reinoso wrote:



  Hi all,

  I have those two classes:

  class Playlist(rdb.Model):
     Represents playlist. It may contain playlist items
     rdb.metadata(metadata)
     rdb.tablename(playlists)

     id = Column(id, Integer, primary_key=True)
     title = Column(title, String(50))
     pending = Column(pending, Boolean)

     items = relationship(PlaylistItem, cascade=all, delete,
  backref=playlists)
     screens = relationship(Screen, secondary=playlist_screens,
  backref=playlists)

  class PlaylistItem(rdb.Model):
     Represents a playlist of items in Schedule page
     rdb.metadata(metadata)
     rdb.tablename(playlist_items)

     id = Column(id, Integer, primary_key=True)
     title = Column(title, String(50))
     runtime = Column(runtime, Integer)
     layoutId = Column(layout_id, Integer, ForeignKey(layouts.id))
     playlistId = Column(playlist_id, Integer,
  ForeignKey(playlists.id))

     layout = relationship(Layout, uselist=False)
     playlist = relationship(Playlist, uselist=False)

  One playlist can contain many PlaylistItem and PlaylistItem could
  contain layout or another playlist. The problem is when adding a
  PlaylistItem to a Playlist, PlaylistItem automatically gets the id of
  its parent (playlist_id). How can I avoid this?

  Thanks in advance!

 the items collection of Playlist represents a one-to-many reference to a 
 list of PlaylistItems.   A PlaylistItem in turn can only be referenced by one 
 parent.   The items/playlists relationships therefore manage the playlistId 
 attribute to be the one foreign key represented by this ownership.    If a 
 PlaylistItem is capable of having multiple Playlist parents, this 
 relationship should be changed to a many-to-many.    Reference on 
 relationship() patterns is 
 athttp://www.sqlalchemy.org/docs/orm/relationships.html#basic-relationa



  --
  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 
  athttp://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 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.



Re: [sqlalchemy] Re: Managing transactions from Session using raw_connections

2010-12-07 Thread Michael Bayer
when I answered your email I realized we should probably add a bind argument 
to connection() and execute(), for those cases where you have the actual bind 
(and the kw, for subclass situations). ticket #1996





On Dec 7, 2010, at 7:32 AM, Ralph Heinkel wrote:

 Hi Michael,
 
 thanks for your help.
 Since some of my engines are not bound to mappers I've tried the
 approach suggested in your previous post to subclass Session so that
 'get_bind' would accept an additional 'engine' argument. This works
 fine with Session.execute() which passes **kw down to the 'get_bind'
 method, but unfortunately Session.connection() doesn't. Is this
 omitted intentionally, or could it be added?
 
 Thanks a lot,
 
 Ralph
 
 Here you'd use Session.execute() and Session.connection() to get at the 
 Connection you'd normally get from engine.contextual_connect() 
 (http://www.sqlalchemy.org/docs/orm/session.html#using-sql-expressions...) . 
execute() and connection() accept a mapper argument for the case where 
 individual engines are associated with individual mappers, and both 
 ultimately call get_bind() which you can override via subclass if desired to 
 accept other kinds of arguments.
 
 
 -- 
 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.
 

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



Re: [sqlalchemy] Re: Trouble detaching all objects with relationships intact

2010-12-07 Thread Michael Bayer

On Dec 7, 2010, at 7:46 AM, Ian Thompson wrote:

 On Dec 6, 5:53 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Dec 6, 2010, at 11:52 AM, Ian Thompson wrote:
 
 
 
 
 
 On Dec 6, 4:49 pm, Ian Thompson quorn...@gmail.com wrote:
 I've generated a test data set and then want to have all of the
 created objects and relationships available for reference easily.
 To avoid possible pollution of this fixture I want to detach the
 objects from the sessions to avoid them getting any updates applied.
 
 To do this I am attempting to load all data via joinedload and then
 calling session.expunge_all:
 
 print assets['test_asset_1'].attributes # OK
 
 session.query(Asset).options(joinedload(Asset.asset_attributes)).populate_e
  xisting().all()
 session.expunge_all()
 print assets['test_asset_1'].attributes # ERROR
 
 None of the relationships on the objects survive the expunge.
 
 sqlalchemy.orm.exc.DetachedInstanceError:
 Parent instance Asset at ... is not bound to a Session;
 lazy load operation of attribute 'asset_attributes' cannot proceed
 (cont...)
 Is there a correct way to detach all current data from the Session
 fully loading any relationships? (Small data set so memory is not a
 concern.)
 
 This use case seems like it would be more easily solved using a new Session 
 just for that load.   To work with the objects detached, all deferred 
 attributes and lazy-loading relationship attributes which you will need to 
 access would need to either be eagerly loaded (there's now three ways to 
 eagerly load relationship() attributes), or otherwise accessed via 
 obj.attribute before the objects are expunged.
 
 Hi Michael, thanks for your reply.
 
 I had thought by using joinedload (or eagerload) and populate_existing
 I would be doing an eager load of the specified relation.
 
 Also, in my example I do access the attributes relationship (with a
 print), then after the expunge the same fails. Is there a way I can
 ensure the data is preserved after expunging?

expunge() doesn't affect the data associated with the instances.   Its possible 
the populate_existing() is interfering with the joined load, I'd check the 
status of asset_attributes right after the query.



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

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



Re: [sqlalchemy] Re: Problem with one relation

2010-12-07 Thread Michael Bayer

On Dec 7, 2010, at 10:09 AM, Alvaro Reinoso wrote:

 The relation is OK, one-to-many between Playlist and PlaylisItem.
 However, PlaylistItem can contain one Playlist or one Layout and that
 Playlist could be in many PlaylistItems. I know it's weird relation if
 I already have Playlist as parent of PlaylistItem, but could I get
 this?

If you mean, you want PlaylistItem.playlist to be independent of 
Playlist.items, just use two different foreign keys and two different 
relationships.   Otherwise I don't really have enough detail here to know what 
you're asking for.



 
 Thanks!
 
 On Dec 6, 3:51 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Dec 6, 2010, at 2:39 PM, Alvaro Reinoso wrote:
 
 
 
 Hi all,
 
 I have those two classes:
 
 class Playlist(rdb.Model):
Represents playlist. It may contain playlist items
rdb.metadata(metadata)
rdb.tablename(playlists)
 
id = Column(id, Integer, primary_key=True)
title = Column(title, String(50))
pending = Column(pending, Boolean)
 
items = relationship(PlaylistItem, cascade=all, delete,
 backref=playlists)
screens = relationship(Screen, secondary=playlist_screens,
 backref=playlists)
 
 class PlaylistItem(rdb.Model):
Represents a playlist of items in Schedule page
rdb.metadata(metadata)
rdb.tablename(playlist_items)
 
id = Column(id, Integer, primary_key=True)
title = Column(title, String(50))
runtime = Column(runtime, Integer)
layoutId = Column(layout_id, Integer, ForeignKey(layouts.id))
playlistId = Column(playlist_id, Integer,
 ForeignKey(playlists.id))
 
layout = relationship(Layout, uselist=False)
playlist = relationship(Playlist, uselist=False)
 
 One playlist can contain many PlaylistItem and PlaylistItem could
 contain layout or another playlist. The problem is when adding a
 PlaylistItem to a Playlist, PlaylistItem automatically gets the id of
 its parent (playlist_id). How can I avoid this?
 
 Thanks in advance!
 
 the items collection of Playlist represents a one-to-many reference to a 
 list of PlaylistItems.   A PlaylistItem in turn can only be referenced by 
 one parent.   The items/playlists relationships therefore manage the 
 playlistId attribute to be the one foreign key represented by this 
 ownership.If a PlaylistItem is capable of having multiple Playlist 
 parents, this relationship should be changed to a many-to-many.Reference 
 on relationship() patterns is 
 athttp://www.sqlalchemy.org/docs/orm/relationships.html#basic-relationa
 
 
 
 --
 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 
 athttp://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 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.
 

-- 
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] table inheritance

2010-12-07 Thread Tarek Ziadé
Hey,

I have a declarative table called 'Foo':

_Base = declarative_base()

class Foo(_Base):
__tablename__ = 'foo'
__table_args__ = {'mysql_engine': 'InnoDB',
  'mysql_charset': 'latin1'}
id = Column(String(64), primary_key=True, autoincrement=False)
stuff = Column(Integer(11), primary_key=True, nullable=False)

Now I create Foo1 that has exactly the same definition but differs
just by the name:

class Foo1(_Base):
__tablename__ = 'foo1'
__table_args__ = {'mysql_engine': 'InnoDB',
  'mysql_charset': 'latin1'}
id = Column(String(64), primary_key=True, autoincrement=False)
stuff = Column(Integer(11), primary_key=True, nullable=False)


And I need more of those (FooN with N = 10). The use case is to shard
data across several tables.

To avoid cut/n/pastes, I tried inheritance,  meta-class, a type
factory and the like, but everything fails. Whether because I was
reusing the same column class objects or whether because I was
conflicting with SQLALchemy meta-class magic.

Is there any sane way to do this and avoid a crazy copy/paste ?

Cheers
Tarek

-- 
Tarek Ziadé | http://ziade.org

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



Re: [sqlalchemy] table inheritance

2010-12-07 Thread Michael Bayer
This is the entity name recipe, and we have a classical and declarative 
version over at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/EntityName .   
 It's using just the straight type() factory. 

You definitely want to use distinct Column objects for each class, this because 
a Column object is immediately made to reference its parent Table, for obvious 
reasons.   Recipes that generate multiple tables from a single set of Column 
objects use the copy() method on Column for this purpose.   However, you don't 
even need to deal with that, since declarative mixins take care of the copy() 
stuff for you.  I've updated the EntityName example to illustrate taking 
advantage of the mixin so check it out.




On Dec 7, 2010, at 10:45 AM, Tarek Ziadé wrote:

 Hey,
 
 I have a declarative table called 'Foo':
 
 _Base = declarative_base()
 
 class Foo(_Base):
__tablename__ = 'foo'
__table_args__ = {'mysql_engine': 'InnoDB',
  'mysql_charset': 'latin1'}
id = Column(String(64), primary_key=True, autoincrement=False)
stuff = Column(Integer(11), primary_key=True, nullable=False)
 
 Now I create Foo1 that has exactly the same definition but differs
 just by the name:
 
 class Foo1(_Base):
__tablename__ = 'foo1'
__table_args__ = {'mysql_engine': 'InnoDB',
  'mysql_charset': 'latin1'}
id = Column(String(64), primary_key=True, autoincrement=False)
stuff = Column(Integer(11), primary_key=True, nullable=False)
 
 
 And I need more of those (FooN with N = 10). The use case is to shard
 data across several tables.
 
 To avoid cut/n/pastes, I tried inheritance,  meta-class, a type
 factory and the like, but everything fails. Whether because I was
 reusing the same column class objects or whether because I was
 conflicting with SQLALchemy meta-class magic.
 
 Is there any sane way to do this and avoid a crazy copy/paste ?
 
 Cheers
 Tarek
 
 -- 
 Tarek Ziadé | http://ziade.org
 
 -- 
 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.
 

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



Re: [sqlalchemy] table inheritance

2010-12-07 Thread Tarek Ziadé
On Tue, Dec 7, 2010 at 5:08 PM, Michael Bayer mike...@zzzcomputing.com wrote:
 This is the entity name recipe, and we have a classical and declarative 
 version over at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/EntityName . 
    It's using just the straight type() factory.

 You definitely want to use distinct Column objects for each class, this 
 because a Column object is immediately made to reference its parent Table, 
 for obvious reasons.   Recipes that generate multiple tables from a single 
 set of Column objects use the copy() method on Column for this purpose.   
 However, you don't even need to deal with that, since declarative mixins take 
 care of the copy() stuff for you.  I've updated the EntityName example to 
 illustrate taking advantage of the mixin so check it out.

Thanks !

I am always amazed by how fast and accurately you are always answering
here, kudos for this.


Cheers
Tarek
-- 
Tarek Ziadé | http://ziade.org

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



Re: [sqlalchemy] Re: Managing transactions from Session using raw_connections

2010-12-07 Thread Michael Bayer
I hadn't planned any future 0.5 releases, 0.7 is almost ready for betas.  What 
are the incompatibilities you have with 0.6 ?


On Dec 7, 2010, at 11:58 AM, Ralph Heinkel wrote:

 This would be great! Thanks, Michael!
 
 (is this also going into the 0.5.x branch - we are not yet able to
 upgrade to 0.6.x due to incompatibilities...)
 
 Ralph
 
 On Dec 7, 4:09 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 when I answered your email I realized we should probably add a bind 
 argument to connection() and execute(), for those cases where you have the 
 actual bind (and the kw, for subclass situations). ticket #1996
 
 On Dec 7, 2010, at 7:32 AM, Ralph Heinkel wrote:
 
 Hi Michael,
 
 thanks for your help.
 Since some of my engines are not bound to mappers I've tried the
 approach suggested in your previous post to subclass Session so that
 'get_bind' would accept an additional 'engine' argument. This works
 fine with Session.execute() which passes **kw down to the 'get_bind'
 method, but unfortunately Session.connection() doesn't. Is this
 omitted intentionally, or could it be added?
 
 Thanks a lot,
 
 Ralph
 
 Here you'd use Session.execute() and Session.connection() to get at the 
 Connection you'd normally get from engine.contextual_connect() 
 (http://www.sqlalchemy.org/docs/orm/session.html#using-sql-expressions...) 
 .execute() and connection() accept a mapper argument for the case 
 where individual engines are associated with individual mappers, and both 
 ultimately call get_bind() which you can override via subclass if desired 
 to accept other kinds of arguments.
 
 --
 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 
 athttp://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 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.
 

-- 
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] composite property not working on a mapped select

2010-12-07 Thread Will

Hello,

I've been trying to map two tables that have 25 columns each in
addition to
keys.  The parent table's (descriptions) column values represent the
key in a
dictionary and the child table's (values) column values represent the
values of
the dictionary.

i.e.:

table 'descriptions':
id SERIAL
description1 Text
description2 Text
...
description25 Text

table 'values':
id SERIAL
descriptions_id INTEGER NOT NULL FOREIGN KEY descriptions.id
value1 Text
value2 Text
...
value25 Text


I wanted to map the join of the descriptions table and the values
table to a
single class and make two composite properties that would represent
the 50
columns.

Below I stared to do a prototype of this and ran into a problem with
composite
properties not working on mapped join (select alias). I never got
around to
making the dictionary I planned.

And I know that the design of the descriptions table and values table
is not
the best but it's what I have to work with at this point.


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, mapper, relationship,
composite
from sqlalchemy.schema import Table, Column, MetaData, ForeignKey
from sqlalchemy.sql import select
from sqlalchemy.types import Integer, Text

engine = create_engine('sqlite:///')
session = sessionmaker(bind=engine, autocommit=True)()
metadata = MetaData(engine)


# Tables
descriptions_table = Table('descriptions', metadata,
Column('id', Integer, primary_key=True),
Column('d1', Text),
Column('d2', Text),
)

values_table = Table('values', metadata,
Column('id', Integer, primary_key=True),
Column('description_id', Integer, ForeignKey('descriptions.id'),
   nullable=False),
Column('v1', Text),
Column('v2', Text),
)

desc_values = select(
[values_table, descriptions_table.c.d1, descriptions_table.c.d2],
descriptions_table.c.id == values_table.c.description_id
).alias('descriptions_values')


# Classes
class Descriptions(object):
pass


class Values(object):
pass


class CustomValues(list):

def __init__(self, *args):
self.extend(args)

def __composite_values__(self):
return self


# Mappers
mapper(Descriptions, descriptions_table, properties={
'values': relationship(Values, lazy='dynamic'),
'custom_descriptions': composite(CustomValues,
descriptions_table.c.d1,
 descriptions_table.c.d2),
})

mapper(Values, desc_values, properties={
'custom_descriptions': composite(CustomValues, desc_values.c.v1,
 desc_values.c.v2),
})


# Testing
metadata.create_all()
engine.echo = True


descriptions = Descriptions()
descriptions.custom_descriptions = CustomValues('Color', 'Number')

values1 = Values()
values1.custom_values = CustomValues('Red', '5')

values2 = Values()
values2.custom_values = CustomValues('Blue', '1')

descriptions.values.append(values1)
descriptions.values.append(values2)

session.add(descriptions)
session.flush()


# Output

2010-12-07 13:32:04,933 INFO sqlalchemy.engine.base.Engine.0x...e690
BEGIN (implicit)
2010-12-07 13:32:04,933 INFO sqlalchemy.engine.base.Engine.0x...e690
INSERT INTO descriptions (d1, d2) VALUES (?, ?)
2010-12-07 13:32:04,933 INFO sqlalchemy.engine.base.Engine.0x...e690
('Color', 'Number')
2010-12-07 13:32:04,934 INFO sqlalchemy.engine.base.Engine.0x...e690
INSERT INTO values (description_id, v1, v2) VALUES (?, ?, ?)
2010-12-07 13:32:04,934 INFO sqlalchemy.engine.base.Engine.0x...e690
(1, None, None)
2010-12-07 13:32:04,934 INFO sqlalchemy.engine.base.Engine.0x...e690
INSERT INTO values (description_id, v1, v2) VALUES (?, ?, ?)
2010-12-07 13:32:04,935 INFO sqlalchemy.engine.base.Engine.0x...e690
(1, None, None)
2010-12-07 13:32:04,935 INFO sqlalchemy.engine.base.Engine.0x...e690
COMMIT


From this ouput we can see that v1 and v2 are not being set, they are
being
left at None, but descriptions.custom_descriptions IS being set
properly.

I'm hoping it's just something I missed.


-- 
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: Problem with one relation

2010-12-07 Thread Alvaro Reinoso
Yes, that's what I want. I already have the two relations that I
want.:

One is in Playlist,   items = relationship(PlaylistItem,
cascade=all, delete, backref=playlists).
If I understand properly, this allows Playlist to contain
PlaylistItems

And the another one is in PlaylistItem, playlist =
relationship(Playlist, uselist=False).
This one is the one that allows the PlaylistItem to contain objects of
Playlist type. (Or at least, that's what I'd like it to be). This
shouldn't be pointing to its Playlist parent, but to a different
Playlist (which this item contains, because a Playlist can contain
other Playlists inside) I guess for that I'd need another
ForeignKey(playlists.id), but I don't really know how to
differentiate this ForeignKey from the one that is pointing to its
parent Playlist object (or record).

Thank you for your help, btw

On Dec 7, 10:16 am, Michael Bayer mike...@zzzcomputing.com wrote:
 On Dec 7, 2010, at 10:09 AM, Alvaro Reinoso wrote:

  The relation is OK, one-to-many between Playlist and PlaylisItem.
  However, PlaylistItem can contain one Playlist or one Layout and that
  Playlist could be in many PlaylistItems. I know it's weird relation if
  I already have Playlist as parent of PlaylistItem, but could I get
  this?

 If you mean, you want PlaylistItem.playlist to be independent of 
 Playlist.items, just use two different foreign keys and two different 
 relationships.   Otherwise I don't really have enough detail here to know 
 what you're asking for.



  Thanks!

  On Dec 6, 3:51 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  On Dec 6, 2010, at 2:39 PM, Alvaro Reinoso wrote:

  Hi all,

  I have those two classes:

  class Playlist(rdb.Model):
     Represents playlist. It may contain playlist items
     rdb.metadata(metadata)
     rdb.tablename(playlists)

     id = Column(id, Integer, primary_key=True)
     title = Column(title, String(50))
     pending = Column(pending, Boolean)

     items = relationship(PlaylistItem, cascade=all, delete,
  backref=playlists)
     screens = relationship(Screen, secondary=playlist_screens,
  backref=playlists)

  class PlaylistItem(rdb.Model):
     Represents a playlist of items in Schedule page
     rdb.metadata(metadata)
     rdb.tablename(playlist_items)

     id = Column(id, Integer, primary_key=True)
     title = Column(title, String(50))
     runtime = Column(runtime, Integer)
     layoutId = Column(layout_id, Integer, ForeignKey(layouts.id))
     playlistId = Column(playlist_id, Integer,
  ForeignKey(playlists.id))

     layout = relationship(Layout, uselist=False)
     playlist = relationship(Playlist, uselist=False)

  One playlist can contain many PlaylistItem and PlaylistItem could
  contain layout or another playlist. The problem is when adding a
  PlaylistItem to a Playlist, PlaylistItem automatically gets the id of
  its parent (playlist_id). How can I avoid this?

  Thanks in advance!

  the items collection of Playlist represents a one-to-many reference to a 
  list of PlaylistItems.   A PlaylistItem in turn can only be referenced by 
  one parent.   The items/playlists relationships therefore manage the 
  playlistId attribute to be the one foreign key represented by this 
  ownership.    If a PlaylistItem is capable of having multiple Playlist 
  parents, this relationship should be changed to a many-to-many.    
  Reference on relationship() patterns is 
  athttp://www.sqlalchemy.org/docs/orm/relationships.html#basic-relationa

  --
  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 
  athttp://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 sqlalch...@googlegroups.com.
  To unsubscribe from this group, send email to 
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group 
  athttp://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 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.



Re: [sqlalchemy] Failing test - SQLAlchemy 0.5.8, python 2.7.0

2010-12-07 Thread Martin Bacovsky
On Thursday, November 25, 2010 02:29 am, Michael Bayer wrote 
 well I'd just take those tests out of 0.5 , I'm not sure if theres any
 other way to affect the outcome of nosetests without modifying SQLA code
 or tests directly.
I finally had time to resolve this. Link to my patch (just for tracking 
purposes):
http://pkgs.fedoraproject.org/gitweb/?p=python-sqlalchemy0.5.git;a=blob;f=python-sqlalchemy0.5-
py27.patch;h=6946e846f1430bb9d7dc1137eea9afa8e7eb822e;hb=HEAD

If anyone has less awkward solution, please let me know.

Regards,
Martin


 
 On Nov 24, 2010, at 8:17 PM, Martin Bacovsky wrote:
  On Thursday, November 25, 2010 01:41 am, Michael Bayer wrote
  
  there might be some dictionary ordering dependencies in those tests,
  which will not behave consistently across platforms.
  
  if they fail with 0.6.5 as well, let me know and we'll file a ticket.
  There aren't any 0.5 releases planned.
  
  Thanks for the quick response.
  With 0.6.5 I didn't noticed this issue, but I need to use 0.5.x because
  of some dependences. I suspect dictionary ordering too, but it seems to
  be somewhere in statement compiler which I don't feel like to touch ;).
  I'll rather try to add some workaround in the tests.
  
  Regards,
  Martin
  
  On Nov 24, 2010, at 7:06 PM, Martin Bacovsky wrote:
  Hi,
  I'm trying to build SqlAlchemy 0.5.8 (Linux, python 2.7.0).
  From time to time some of the tests fail.
  
  - the sources are always the same
  - so far the only affected tests were those testing compilation of
  statements with joined tables - the orderning of the tables in the from
  clause seems to be random - sometimes match the expected stmt sometimes
  not - it also seems that on different machines different tests tend to
  fail :O.
  
  Excerpt from buildlog showing the behaviour is attached.
  
  Does anyone have met this issue? Any idea how to deal with it?
  
  For the sake of completeness I add that there was also test failing on
  @profiling.function_call_count which I fixed by adding expected numbers
  for python 2.7. But I don't expect it is related.
  
  Regards,
  Martin
  
  nosetests
  ...
  ..
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ..
  ...
  ...F..
  ... .
  ==
  FAIL: test.sql.test_select.SelectTest.test_binds
  --
  
  Traceback (most recent call last):
   File /usr/lib/python2.7/site-packages/nose/case.py, line 186, in
   runTest
   
 self.test(*self.arg)
   
   File
   /builddir/build/BUILD/SQLAlchemy-0.5.8/lib/sqlalchemy/test/testing.py
   , line 368, in safe
   
 return fn(*args, **kw)
   
   File /builddir/build/BUILD/SQLAlchemy-0.5.8/test/sql/test_select.py,
   line 1193, in test_binds
   
 self.assert_compile(stmt, expected_named_stmt,
 params=expected_default_params_dict)
   
   File
   

Re: [sqlalchemy] composite property not working on a mapped select

2010-12-07 Thread Michael Bayer
its a bug, and theres a new ticket http://www.sqlalchemy.org/trac/ticket/1997 
with a small patch.

I would suggest maybe not using composites for this for now.   Most of what 
composite does you can accomplish using descriptors:

class Value(object):
@property
def custom_values(self):
return CustomValue(self.v1, self.v2)

@custom_values.setter
def custom_values(self, value):
self.v1 = value.v1
self.v2 = value.v2

I considered replacing the mechanism of composite with the above type of thing 
in 0.7 but there were still some edge cases that composites seem to have.



On Dec 7, 2010, at 1:54 PM, Will wrote:

 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker, mapper, relationship,
 composite
 from sqlalchemy.schema import Table, Column, MetaData, ForeignKey
 from sqlalchemy.sql import select
 from sqlalchemy.types import Integer, Text
 
 engine = create_engine('sqlite:///')
 session = sessionmaker(bind=engine, autocommit=True)()
 metadata = MetaData(engine)
 
 
 # Tables
 descriptions_table = Table('descriptions', metadata,
Column('id', Integer, primary_key=True),
Column('d1', Text),
Column('d2', Text),
 )
 
 values_table = Table('values', metadata,
Column('id', Integer, primary_key=True),
Column('description_id', Integer, ForeignKey('descriptions.id'),
   nullable=False),
Column('v1', Text),
Column('v2', Text),
 )
 
 desc_values = select(
[values_table, descriptions_table.c.d1, descriptions_table.c.d2],
descriptions_table.c.id == values_table.c.description_id
 ).alias('descriptions_values')
 
 
 # Classes
 class Descriptions(object):
pass
 
 
 class Values(object):
pass
 
 
 class CustomValues(list):
 
def __init__(self, *args):
self.extend(args)
 
def __composite_values__(self):
return self
 
 
 # Mappers
 mapper(Descriptions, descriptions_table, properties={
'values': relationship(Values, lazy='dynamic'),
'custom_descriptions': composite(CustomValues,
 descriptions_table.c.d1,
 descriptions_table.c.d2),
 })
 
 mapper(Values, desc_values, properties={
'custom_descriptions': composite(CustomValues, desc_values.c.v1,
 desc_values.c.v2),
 })
 
 
 # Testing
 metadata.create_all()
 engine.echo = True
 
 
 descriptions = Descriptions()
 descriptions.custom_descriptions = CustomValues('Color', 'Number')
 
 values1 = Values()
 values1.custom_values = CustomValues('Red', '5')
 
 values2 = Values()
 values2.custom_values = CustomValues('Blue', '1')
 
 descriptions.values.append(values1)
 descriptions.values.append(values2)
 
 session.add(descriptions)
 session.flush()

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