[sqlalchemy] Re: Correct way of moving relation

2009-01-30 Thread Werner F. Bruhin

Michael,

Michael Bayer wrote:
 oh, duh.  do it like this:

 for purchase in list(aItem.purchase):
   purchase.cbbottle = bItem

 I leave it to you as an exercise why this is the case.
   
aItem.purchase is an instrumented list and as such is mutable, is that 
the right conclusion?

Thanks for your help
Werner

--~--~-~--~~~---~--~~
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] Re: Getting ForeignKey Before Commit

2009-01-30 Thread Werner F. Bruhin

n00b wrote:
 why don't you work off the las/previous committed rec id?

 On Jan 29, 4:05 am, Dejan Mayo dejan.m...@gmail.com wrote:
   
 Hi,

 My code is like that:

 try:
 for some_val in some_values:
 rec = SomeModel()
 rec.some_val = some_val
 session.save(rec)
 session.commit()
 except:
 session.rollback()

 For each record that I'm creating, I need to send an email right
 after. And each email includes some data about these records,
 especially the rec.id. Everything looks fine but the problem is
 getting the rec.id.

 I've tried:

 try:
 for some_val in some_values:
 rec = SomeModel()
 rec.some_val = some_val
 session.save(rec)
 email(rec=rec) # rec doesn't have id yet so it doesn't work
 
what about doing:
session.flush()
here?

Werner

--~--~-~--~~~---~--~~
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] Using orderedlist with a secondary table

2009-01-30 Thread Nathan Harmston
Hi,

I am currently trying to make a relation between Document and Author, where
there is a many to many association which is dealt with by a secondary table
and I am trying to store the position of an author in the author list.
mapper(Paper, document_table, properties={'journal':relation(Journal,
backref='paper'),
  'authors':relation(Author,
secondary=papers_to_authors_table,

collection_class=ordering_list('position'),

order_by=[papers_to_authors_table.c.position]),
  'keywords':relation(Keyword,
secondary=documents_to_keywords_table)
  })

papers_to_authors_table = Table('p2a_association', metadata,
Column('document', Integer,
ForeignKey('documents.document_id')),
Column('author', Integer,
ForeignKey('authors.author_id')),
Column('position', Integer)
)

However when I try to do this:
 File Worker.py, line 281, in module
p = Paper(title, date, authors, journal, j_volume, j_issue, keywords,
abstract, None, body, pmid, institution)
  File
/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/orm/attributes.py,
line 1218, in init
oldinit(instance, *args, **kwargs)
  File /cluster/home/nathan/work/workspace/txtmining/Registry.py, line
258, in __init__
self.authors.append(a)
  File
/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/orm/collections.py,
line 922, in append
fn(self, item)
  File
/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/ext/orderinglist.py,
line 214, in append
self._order_entity(len(self) - 1, entity, self.reorder_on_append)
  File
/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/ext/orderinglist.py,
line 202, in _order_entity
have = self._get_order_value(entity)
  File
/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/ext/orderinglist.py,
line 189, in _get_order_value
return getattr(entity, self.ordering_attr)
AttributeError: 'Author' object has no attribute 'position'

So is there an easy way of performing this functionality using the ordered
list or do I need to do something else?

Many thanks in advance

nathan

--~--~-~--~~~---~--~~
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] Re: Using orderedlist with a secondary table

2009-01-30 Thread King Simon-NFHD78

 -Original Message-
 From: sqlalchemy@googlegroups.com 
 [mailto:sqlalch...@googlegroups.com] On Behalf Of Nathan Harmston
 Sent: 30 January 2009 13:15
 To: sqlalchemy@googlegroups.com
 Subject: [sqlalchemy] Using orderedlist with a secondary table
 
 Hi, 
 
 I am currently trying to make a relation between Document and 
 Author, where there is a many to many association which is 
 dealt with by a secondary table and I am trying to store the 
 position of an author in the author list.


[snip]


 So is there an easy way of performing this functionality 
 using the ordered list or do I need to do something else?
 
 Many thanks in advance
 
 nathan
 
 

You can't use the 'secondary' mapper argument if you want to be able to
use columns in that secondary table. Instead, you need to map a class to
your secondary table directly, such that it has 1-many relations with
the Document and Author classes. You can then use the 'associationproxy'
extension to hide the details.

See the docs at
http://www.sqlalchemy.org/docs/05/mappers.html#association-pattern and
http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.html#as
sociationproxy

Hope that helps,

Simon

--~--~-~--~~~---~--~~
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] Re: (InterfaceError) connection already closed

2009-01-30 Thread Michael Bayer


On Jan 30, 2009, at 5:50 AM, Alessandro Dentella wrote:


 Hi,

  i'm trying to understand how to cope with errors from the database  
 and I
  don't completely understand the following behaviour.

  I try to delete a Project that has integrity constrains, so  
 correctly the
  db complains and SA raises an OperationalError. Right now I'm not  
 trying
  to understand how to *prevent* this. I just want to understand how  
 to give
  a message to the user and restore a working situation.

  [I'm working with postgreSQL and SA 5.0rc4]


 def delete_obj(obj):
print ### deleting obj, repr(obj)
try:
session.delete(obj)
print session.delete:, session.deleted
session.commit()
except Exception, e:
print Problemi..., e.message
session.rollback()

 q = session.query(Project)
 prj = q.filter_by(id=18).one()

 print repr(prj)
 delete_obj(prj)

 print session.delete:, session.deleted
 for obj in session:
print obj, hex(id(obj)), type(obj)


 print Project, prj  ### this code raises the  
 Interface error


   where is the problem? the connection (as I tend to believe) or the  
 object 'prj'?

   What's the state in which is now the object 'prj'? What should I  
 do to
   go back to the previous situation?

   What exacltly means InterfaceError - connection already closed. I  
 simply
   issued a .rollback(), why should it be closed? How should I test  
 if a
   connection is closed? and how should I open it again?

connection already closed is a psycopg2 error indicating that the  
socket has been shut down.   looking at your output, I see a raw  
execute() occuring within the string conversion of your Project object  
and theres a module called sqlkit/db/utils.py causing an exception  
throw.   SQLA's rollback then fails too but probably for the same  
reason.  since you're executing directly off of the engine there, it  
would seem unrelated to the state of the connection related to the  
session.its not apparent why the socket would be shut down,  
however, unless the database were restarted perhaps.



--~--~-~--~~~---~--~~
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] Re: find only loaded objects in relation collections

2009-01-30 Thread jason kirtland

GHZ wrote:
 Hi,
 
 I have a Subscriber and an Address table.  Subscriber can have many
 Addresses
 
 
 mapper(Subscriber, subscriber_table, properties={
 'addresses' : relation(Address, collection_class=Addresses,
 backref='customer')})
 
 From the a Subscriber object, I want to inspect all loaded objects in
 any collections, but do it quietly - without causing any more to load.
 
 
 class MyBase(object):
 
 @reconstructor
 def __my_init__(self):
 self.rules = []
 
 def get_all_rules_on_all_loaded_related_objects(self):
 for collection in (p for p in object_mapper
 (self).iterate_properties if type(p) is RelationProperty):
 # How to access this collection without causing it to
 load?
 # I want to look at the 'rules' property on all loaded
 objects

The collection will be present in the instance's __dict__ if it has been 
loaded.  So something like

   if 'addresses' in self.__dict__:
  # loaded, can access self.addresses without triggering db access


-j

--~--~-~--~~~---~--~~
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] polymorphic_identity not allowed to be zero for base class?

2009-01-30 Thread Gunnlaugur Thor Briem
Hi,

[trying to send this again, seems like previous copy got lost in some
moderation queue]

Messing with single-table inheritance in a declarative model, with a
non-abstract base class, I find that querying fails if polymorphic_identity
is 0 (zero). Example:

 code begins 
from sqlalchemy import Column, Integer, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
class Course(Base):
__tablename__ = 'course'
id = Column(Integer, primary_key=True)
course_type = Column(Integer)
__mapper_args__ = {'polymorphic_on':course_type,
'polymorphic_identity':0}

class MultiYearCourse(Course):
__mapper_args__ = {'polymorphic_identity':1}

engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(bind=engine)
session = sessionmaker(bind=engine)()

myc = MultiYearCourse()
myc.name = uComputer Graphics
c = Course()
c.name = uSociology
session.add(c)
session.add(myc)
session.commit()

print MYC: %s % myc
print C:   %s % c
query = session.query(Course)
print Query:   %s % query
print Results: %s % query.all()
 code ends 

That last line fails with an AssertionError:

 output begins 
MYC: __main__.MultiYearCourse object at 0xcf7d30
C:   __main__.Course object at 0xcf7d70
Query:   SELECT course.id AS course_id, course.course_type AS
course_course_type
FROM course
Traceback (most recent call last):
  File /Users/gthb/Documents/workspace/test/src/sqlalchemytest.py, line
31, in module
print Results: %s % query.all()
  File
/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py,
line 1186, in all
return list(self)
  File
/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py,
line 1341, in instances
rows = [process[0](context, row) for row in fetch]
  File
/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py,
line 1942, in main
return _instance(row, None)
  File
/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/mapper.py,
line 1557, in _instance
_instance = polymorphic_instances[discriminator]
  File
/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/util.py,
line 71, in __missing__
self[key] = val = self.creator(key)
  File
/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/mapper.py,
line 1695, in configure_subclass_mapper
raise AssertionError(No such polymorphic_identity %r is defined %
discriminator)
AssertionError: No such polymorphic_identity 0 is defined
 output ends 

But if I exchange the polymorphic identities, so the base class gets the 1
and the subclass gets the 0, then it runs just fine!

It seems to me that this can't be intentional — don't see a reason for it,
and the docs do not mention any particular restrictions on values of
polymorphic_identity.

Regards,

- Gulli

--~--~-~--~~~---~--~~
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] Re: polymorphic_identity not allowed to be zero for base class?

2009-01-30 Thread Michael Bayer
its likely a bug.  there are some boolean tests for  
polymorphic_identity in mapper.py which should be changed to is  
None.


On Jan 30, 2009, at 11:58 AM, Gunnlaugur Thor Briem wrote:

 Hi,

 [trying to send this again, seems like previous copy got lost in  
 some moderation queue]

 Messing with single-table inheritance in a declarative model, with a  
 non-abstract base class, I find that querying fails if  
 polymorphic_identity is 0 (zero). Example:

  code begins 
 from sqlalchemy import Column, Integer, create_engine
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.orm import sessionmaker

 Base = declarative_base()
 class Course(Base):
 __tablename__ = 'course'
 id = Column(Integer, primary_key=True)
 course_type = Column(Integer)
 __mapper_args__ = {'polymorphic_on':course_type,  
 'polymorphic_identity':0}

 class MultiYearCourse(Course):
 __mapper_args__ = {'polymorphic_identity':1}

 engine = create_engine('sqlite:///:
 memory:')
 Base.metadata.create_all(bind=engine)
 session = sessionmaker(bind=engine)()

 myc = MultiYearCourse()
 myc.name = uComputer Graphics
 c = Course()
 c.name = uSociology
 session.add(c)
 session.add(myc)
 session.commit()

 print MYC: %s % myc
 print C:   %s % c
 query = session.query(Course)
 print Query:   %s % query
 print Results: %s % query.all()
  code ends 

 That last line fails with an AssertionError:

  output begins 
 MYC: __main__.MultiYearCourse object at 0xcf7d30
 C:   __main__.Course object at 0xcf7d70
 Query:   SELECT course.id AS course_id, course.course_type AS  
 course_course_type
 FROM course
 Traceback (most recent call last):
   File /Users/gthb/Documents/workspace/test/src/sqlalchemytest.py,  
 line 31, in module
 print Results: %s % query.all()
   File /Users/gthb/Library/Python/2.5/site-packages/ 
 SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py, line 1186, in all
 return list(self)
   File /Users/gthb/Library/Python/2.5/site-packages/ 
 SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py, line 1341, in  
 instances
 rows = [process[0](context, row) for row in fetch]
   File /Users/gthb/Library/Python/2.5/site-packages/ 
 SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py, line 1942, in  
 main
 return _instance(row, None)
   File /Users/gthb/Library/Python/2.5/site-packages/ 
 SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/mapper.py, line 1557, in  
 _instance
 _instance = polymorphic_instances[discriminator]
   File /Users/gthb/Library/Python/2.5/site-packages/ 
 SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/util.py, line 71, in  
 __missing__
 self[key] = val = self.creator(key)
   File /Users/gthb/Library/Python/2.5/site-packages/ 
 SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/mapper.py, line 1695, in  
 configure_subclass_mapper
 raise AssertionError(No such polymorphic_identity %r is  
 defined % discriminator)
 AssertionError: No such polymorphic_identity 0 is defined
  output ends 

 But if I exchange the polymorphic identities, so the base class gets  
 the 1 and the subclass gets the 0, then it runs just fine!

 It seems to me that this can't be intentional — don't see a reason  
 for it, and the docs do not mention any particular restrictions on  
 values of polymorphic_identity.

 Regards,

 - Gulli

 


--~--~-~--~~~---~--~~
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] Re: polymorphic_identity not allowed to be zero for base class?

2009-01-30 Thread King Simon-NFHD78

Would None (ie. SQL NULL) be a valid polymorphic identity? 

 -Original Message-
 From: sqlalchemy@googlegroups.com 
 [mailto:sqlalch...@googlegroups.com] On Behalf Of Michael Bayer
 Sent: 30 January 2009 17:06
 To: sqlalchemy@googlegroups.com
 Subject: [sqlalchemy] Re: polymorphic_identity not allowed to 
 be zero for base class?
 
 its likely a bug.  there are some boolean tests for 
 polymorphic_identity in mapper.py which should be changed 
 to is None.
 
 
 On Jan 30, 2009, at 11:58 AM, Gunnlaugur Thor Briem wrote:
 
 
   Hi,
   
   [trying to send this again, seems like previous copy 
 got lost in some moderation queue]
   
   Messing with single-table inheritance in a declarative 
 model, with a non-abstract base class, I find that querying 
 fails if polymorphic_identity is 0 (zero). Example:
   
    code begins 
   from sqlalchemy import Column, Integer, create_engine
   from sqlalchemy.ext.declarative import declarative_base
   from sqlalchemy.orm import sessionmaker
   
   Base = declarative_base()
   class Course(Base):
   __tablename__ = 'course'
   id = Column(Integer, primary_key=True)
   course_type = Column(Integer)
   __mapper_args__ = {'polymorphic_on':course_type, 
 'polymorphic_identity':0}
   
   class MultiYearCourse(Course):
   __mapper_args__ = {'polymorphic_identity':1}
   
   engine = create_engine('sqlite:///:
   memory:')
   Base.metadata.create_all(bind=engine)
   session = sessionmaker(bind=engine)()
   
   myc = MultiYearCourse()
   myc.name http://myc.name/  = uComputer Graphics
   c = Course()
   c.name http://c.name/  = uSociology 
   session.add(c)
   session.add(myc)
   session.commit()
   
   print MYC: %s % myc
   print C:   %s % c
   query = session.query(Course)
   print Query:   %s % query
   print Results: %s % query.all()
    code ends 
   
   That last line fails with an AssertionError:
   
    output begins 
   MYC: __main__.MultiYearCourse object at 0xcf7d30
   C:   __main__.Course object at 0xcf7d70
   Query:   SELECT course.id http://course.id/  AS 
 course_id, course.course_type AS course_course_type 
   FROM course
   Traceback (most recent call last):
 File 
 /Users/gthb/Documents/workspace/test/src/sqlalchemytest.py, 
 line 31, in module
   print Results: %s % query.all()
 File 
 /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2
 -py2.5.egg/sqlalchemy/orm/query.py, line 1186, in all
   return list(self)
 File 
 /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2
 -py2.5.egg/sqlalchemy/orm/query.py, line 1341, in instances
   rows = [process[0](context, row) for row in fetch]
 File 
 /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2
 -py2.5.egg/sqlalchemy/orm/query.py, line 1942, in main
   return _instance(row, None)
 File 
 /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2
 -py2.5.egg/sqlalchemy/orm/mapper.py, line 1557, in _instance
   _instance = polymorphic_instances[discriminator]
 File 
 /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2
 -py2.5.egg/sqlalchemy/util.py, line 71, in __missing__
   self[key] = val = self.creator(key)
 File 
 /Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2
 -py2.5.egg/sqlalchemy/orm/mapper.py, line 1695, in 
 configure_subclass_mapper
   raise AssertionError(No such polymorphic_identity 
 %r is defined % discriminator)
   AssertionError: No such polymorphic_identity 0 is defined
    output ends 
   
   But if I exchange the polymorphic identities, so the 
 base class gets the 1 and the subclass gets the 0, then it 
 runs just fine!
   
   It seems to me that this can't be intentional - don't 
 see a reason for it, and the docs do not mention any 
 particular restrictions on values of polymorphic_identity.
   
   Regards,
   
   - Gulli
 
 
 
 
 
 
  
 
 

--~--~-~--~~~---~--~~
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] Re: polymorphic_identity not allowed to be zero for base class?

2009-01-30 Thread Michael Bayer


On Jan 30, 2009, at 12:19 PM, King Simon-NFHD78 wrote:


 Would None (ie. SQL NULL) be a valid polymorphic identity?

it becomes that issue where we just need some kind of constant to  
represent THIS_IS_NOT_DEFINED, so that we know when  
polymorphic_identity is defined or not.   it's a straightforward  
patch.   Although we'd also need to adapt some expressions such as  
identity IN (x, y, z) to account for NULL, so, supporting NULL is a  
little more work.


--~--~-~--~~~---~--~~
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] Re: polymorphic_identity not allowed to be zero for base class?

2009-01-30 Thread King Simon-NFHD78

 -Original Message-
 From: sqlalchemy@googlegroups.com 
 [mailto:sqlalch...@googlegroups.com] On Behalf Of Michael Bayer
 Sent: 30 January 2009 17:25
 To: sqlalchemy@googlegroups.com
 Subject: [sqlalchemy] Re: polymorphic_identity not allowed to 
 be zero for base class?
 
 
 
 On Jan 30, 2009, at 12:19 PM, King Simon-NFHD78 wrote:
 
 
  Would None (ie. SQL NULL) be a valid polymorphic identity?
 
 it becomes that issue where we just need some kind of constant to  
 represent THIS_IS_NOT_DEFINED, so that we know when  
 polymorphic_identity is defined or not.   it's a straightforward  
 patch.   Although we'd also need to adapt some expressions such as  
 identity IN (x, y, z) to account for NULL, so, supporting NULL is a  
 little more work.
 
 

Thanks - I was only asking out of curiosity. I have no need for it
personally.

Cheers,

Simon

--~--~-~--~~~---~--~~
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] Re: (InterfaceError) connection already closed

2009-01-30 Thread Alessandro Dentella

 connection already closed is a psycopg2 error indicating that the  
 socket has been shut down.   looking at your output, I see a raw  
 execute() occuring within the string conversion of your Project object  
 and theres a module called sqlkit/db/utils.py causing an exception  
 throw.   SQLA's rollback then fails too but probably for the same  
 reason.  since you're executing directly off of the engine there, it  
 would seem unrelated to the state of the connection related to the  
 session.its not apparent why the socket would be shut down,  
 however, unless the database were restarted perhaps.

sorry for not seeing that error myself, on the other hand: no postgres has
never been shut down. Anyhow I got rid of that code (an on the fly __str__
builder), I go little further but get errors again.

  1. prj is fetched from db by a session.query()   id=18
  2. I try to delete it, failing due to constraints on the db
  3. session.rollback()

  4. print prj.name  - error 
  4b. session.expunge_all()
  session.flush()
  fetch a new one (id=20) --  error (reported below on id=18)

So back to the question: in which state is 'prj' after failing deletion and
after rolling back?

Even if I 'session.expunge(prj)' I cannot refetch a new project (even a
different one...)

I'm really puzzled...

sandro
*:-)


def delete_obj(obj):
print ### deleting obj, repr(obj)
try:
session.delete(obj)
print session.delete:, session.deleted
session.commit()
except Exception, e:
print Problems..., e.message
session.rollback()
#session.bind.connect()

q = session.query(Project)
prj = q.filter_by(id=18).one()

print repr(prj)
delete_obj(prj)

print session.deleted:, session.deleted

for obj in session:
print obj, hex(id(obj)), type(obj)

session.expunge_all()
session.flush()


prj = q.filter_by(id=20).one()   ## a different project



##

san...@bluff:/misc/src/hg/py/fossati/fossati$ python test.py
models.ticket.Project object at 0x88a4d8c
### deleting obj models.ticket.Project object at 0x88a4d8c
session.delete: IdentitySet([models.ticket.Project object at 0x88a4d8c])
Problems... (OperationalError) ERROR:  update or delete on table 
ticket_project violates foreign key constraint 
timereport_report_job_id_fkey on table timereport_report
DETAIL:  Key (id)=(18) is still referenced from table timereport_report.

session.deleted: IdentitySet([])
obj 0x88a4d8c class 'models.ticket.Project'
obj 0x88b6c4c class 'models.ticket.Organization'
obj 0x88b61cc class 'models.cliente.User'
No handlers could be found for logger sqlalchemy.pool.QueuePool.0x...cb8c
Traceback (most recent call last):
  File test.py, line 36, in module
prj = q.filter_by(id=20).one()
  File /home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/orm/query.py, 
line 1061, in one
ret = list(self[0:2])
  File /home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/orm/query.py, 
line 961, in __getitem__
return list(res)
  File /home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/orm/query.py, 
line 1101, in __iter__
return self._execute_and_instances(context)
  File /home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/orm/query.py, 
line 1104, in _execute_and_instances
result = self.session.execute(querycontext.statement, params=self._params, 
mapper=self._mapper_zero_or_none())
  File 
/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/orm/session.py, line 
755, in execute
clause, params or {})
  File 
/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py, line 
824, in execute
return Connection.executors[c](self, object, multiparams, params)
  File 
/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py, line 
874, in _execute_clauseelement
return self.__execute_context(context)
  File 
/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py, line 
896, in __execute_context
self._cursor_execute(context.cursor, context.statement, 
context.parameters[0], context=context)
  File 
/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py, line 
950, in _cursor_execute
self._handle_dbapi_exception(e, statement, parameters, cursor, context)
  File 
/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py, line 
931, in _handle_dbapi_exception
raise exc.DBAPIError.instance(statement, parameters, e, 
connection_invalidated=is_disconnect)
sqlalchemy.exc.OperationalError: (OperationalError)   update or delete on table 
ticket_project violates foreign key constraint 
timereport_report_job_id_fkey on table timereport_report
DETAIL:  Key (id)=(18) is still referenced from table timereport_report.
 'SELECT anon_1.ticket_project_id AS anon_1_ticket_project_id, 
anon_1.ticket_project_date_create AS anon_1_ticket_project_date_create, 
anon_1.ticket_project_date_last_modify AS 
anon_1_ticket_project_date_last_modify, 

[sqlalchemy] Filtering lazy='dynamic' collection on class type (single-table inheritance)

2009-01-30 Thread Hans Lellelid

Hi,

I did a quick search but couldn't find the right way to do this in
SA.

For the sake of example, I have a many-to-many relationshp between
Book and Reader.  The 'books' property of reader is loaded using
lazy='dynamic'.  And, finally, Book is mapped with single-table
inheritance and has subclasses for various genres.  -- this is just an
example :)

This works:
reader = session.query(Reader).first()
mysterybooks = reader.books.filter
(Book.genre==MysteryBook.DESCRIMINATOR_KEY)

However, this doesn't feel like the *right* way.  I experimented with
various forms of of_type() but didn't get it to work.  I'm sure I'm
just not fully understanding how that is supposed to work.

Thanks in advance!

Hans
--~--~-~--~~~---~--~~
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] Re: (InterfaceError) connection already closed

2009-01-30 Thread Michael Bayer


On Jan 30, 2009, at 12:38 PM, Alessandro Dentella wrote:


 connection already closed is a psycopg2 error indicating that the
 socket has been shut down.   looking at your output, I see a raw
 execute() occuring within the string conversion of your Project  
 object
 and theres a module called sqlkit/db/utils.py causing an exception
 throw.   SQLA's rollback then fails too but probably for the same
 reason.  since you're executing directly off of the engine there, it
 would seem unrelated to the state of the connection related to the
 session.its not apparent why the socket would be shut down,
 however, unless the database were restarted perhaps.

 sorry for not seeing that error myself, on the other hand: no  
 postgres has
 never been shut down. Anyhow I got rid of that code (an on the fly  
 __str__
 builder), I go little further but get errors again.

  1. prj is fetched from db by a session.query()   id=18
  2. I try to delete it, failing due to constraints on the db
  3. session.rollback()

  4. print prj.name  - error
  4b. session.expunge_all()
  session.flush()
  fetch a new one (id=20) --  error (reported below on id=18)

 So back to the question: in which state is 'prj' after failing  
 deletion and
 after rolling back?

its in the same state as that which it first got loaded, i.e. not  
marked as deleted.  all of its attributes are also expired.the  
stack trace you posted doesn't make sense to me though, as its issuing  
a SELECT statement but PG is raising an exception for an UPDATE / 
DELETE ?  I've never seen that before.  If you can provide a self- 
contained test case which reproduces that behavior we can try it out.


--~--~-~--~~~---~--~~
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] Re: Filtering lazy='dynamic' collection on class type (single-table inheritance)

2009-01-30 Thread Michael Bayer


On Jan 30, 2009, at 1:39 PM, Hans Lellelid wrote:


 Hi,

 I did a quick search but couldn't find the right way to do this in
 SA.

 For the sake of example, I have a many-to-many relationshp between
 Book and Reader.  The 'books' property of reader is loaded using
 lazy='dynamic'.  And, finally, Book is mapped with single-table
 inheritance and has subclasses for various genres.  -- this is just an
 example :)

 This works:
 reader = session.query(Reader).first()
 mysterybooks = reader.books.filter
 (Book.genre==MysteryBook.DESCRIMINATOR_KEY)

 However, this doesn't feel like the *right* way.  I experimented with
 various forms of of_type() but didn't get it to work.  I'm sure I'm
 just not fully understanding how that is supposed to work.

of_type() is currently only implemented for a RelationProperty and  
takes effect for EXISTS subqueries generated by has()/any() and  
query.join().So for the dynamic loader's list of books, what  
you're doing is the best option we have at the moment.   it seems like  
of_type() existing on Query as a generative method would be the way to  
go here...until someone says query(A, B, C).of_type(A).

--~--~-~--~~~---~--~~
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] Re: Filtering lazy='dynamic' collection on class type (single-table inheritance)

2009-01-30 Thread Hans Lellelid



On Jan 30, 2:37 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Jan 30, 2009, at 1:39 PM, Hans Lellelid wrote:





  Hi,

  I did a quick search but couldn't find the right way to do this in
  SA.

  For the sake of example, I have a many-to-many relationshp between
  Book and Reader.  The 'books' property of reader is loaded using
  lazy='dynamic'.  And, finally, Book is mapped with single-table
  inheritance and has subclasses for various genres.  -- this is just an
  example :)

  This works:
  reader = session.query(Reader).first()
  mysterybooks = reader.books.filter
  (Book.genre==MysteryBook.DESCRIMINATOR_KEY)

  However, this doesn't feel like the *right* way.  I experimented with
  various forms of of_type() but didn't get it to work.  I'm sure I'm
  just not fully understanding how that is supposed to work.

 of_type() is currently only implemented for a RelationProperty and  
 takes effect for EXISTS subqueries generated by has()/any() and  
 query.join().    So for the dynamic loader's list of books, what  
 you're doing is the best option we have at the moment.   it seems like  
 of_type() existing on Query as a generative method would be the way to  
 go here...until someone says query(A, B, C).of_type(A).

:) -- Ok, if what I'm doing is right, I'll leave it there.  It does
work fine afterall, but obviously is a little less resilient to me
changing my inheritance mapping strategy down the road.  (Not that I
plan to...)

Thanks!
Hans
--~--~-~--~~~---~--~~
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] Re: Equivalent of UPDATE ... WHERE ... in ORM?

2009-01-30 Thread James

Perfect - thanks Mike!

As I'm using 0.4 (sorry, neglected to mention that) I've gone for the
table.update(...).execute() option and it works a treat.

On Jan 25, 12:25 am, Michael Bayer mike...@zzzcomputing.com wrote:
 the update() method on Query accomplishes this.  Make sure you read  
 the docstring for it which describes some various behaviors you'll  
 want to be aware of.

 alternatively, any SQL expression, like table.update(), UPDATE  
 table can be issued within the ORM's transaction using  
 session.execute().

 On Jan 24, 2009, at 7:14 PM, James wrote:



  Hi,
  Is there a way to update a large number of objects without looping
  through each one, using SA's ORM?

  E.g.
  I want to achieve the following:

  for o in session.query(MyClass).filter_by(prop='some value'):
     o.prop = 'new value'
     session.update(o)

  Without fetching and saving each object from the database. I.e.
  something which would produce SQL like this:

  UPDATE `my_class` SET `prop`='new value' WHERE `prop` = 'some value'

  If not, am I safe mixing and matching ORM operations with SQL
  operations like:
  u = my_classes.update(my_class.c.prop=='some value'), values=
  {'prop':'new value'})?

  Thanks!
  James
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---