[sqlalchemy] Result of a table.update()

2010-10-21 Thread Warwick Prince
Hi All

I'm using 0.6.4 under Windoze with MySQL,  Python 2.6.4 and I had code that I 
thought worked before (0.6.3)  - which appeared to break due to this issue.. I 
could be wrong on this point so I'll just get to the crux of the matter...

I have a result = table.update(whereClause, values=someValuesDict).execute()

The table is updated correctly, however, the resultproxy object I receive as 
'result' appears to have no members.   If I fetchone() or fetchall() I simply 
get a None result.  Is this correct?

If this IS correct, how is the best way to tell if the update was a success?   
I tried putting bad data in the whereClause and it simply did nothing to the 
database, but my resultproxy was the same.  No Exceptions raised in either case?

Please enlighten me..

Cheers
Warwick


Warwick Prince 
Managing Director 
mobile: +61 411 026 992 
skype: warwickprince  
 
phone: +61 7 3102 3730 
fax:  +61 7 3319 6734 
web: www.mushroomsys.com 


-- 
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] Attribute overriding in mixin inheritance hierarchy

2010-10-21 Thread oystein
Hi all,

I am using declarative to model an existing object hierarchy where
there is an
abstract part that i model as an hierarchy of mixins and a concrete
part where
i use joined-table inheritance.

I have a problem when there is attributes with the same name, one
overriding
the other, in the abstract mixin hierarchy. Maybe I am doing something
wrong
so suggestions for how to implement this is most welcome.

Sample code:

import unittest
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, Boolean

Base = declarative_base()

class AbstractA(object):
AttrA = Column(String(50))
def __init__(self, AttrA=AttrA):
self.AttrA = AttrA

class AbstractB(AbstractA):
AttrA = Column(String(50), primary_key=True)
def __init__(self, AttrA, **kwargs):
AbstractA.__init__(self, **kwargs)
self.AttrA = AttrA

class Concrete(Base, AbstractB):
__tablename__ = 'Concrete'
AttrB = Column(Boolean)
def __init__(self, AttrB, **kwargs):
AbstractB.__init__(self, **kwargs)
self.AttrB = AttrB


class TestAttributeOverriding(unittest.TestCase):

def testAttributeOverriding(self):
concrete = Concrete(AttrB = False, AttrA=0)
self.session.add(concrete)
self.session.commit()
assert len(self.session.query(Concrete).all()) == 1

def setUp(self):
self.engine = create_engine('sqlite:///:memory:', echo = True)
Session = sessionmaker(bind = self.engine)
self.session = Session()
self.metadata = Base.metadata
self.metadata.create_all(self.engine)

def tearDown(self):
self.metadata.drop_all(self.engine)
self.session.close()



This would fail with could not assemble any primary key columns for
mapped
table unless i apply the small patch below, which makes it work as i
would
expect (although i don't know if this is how it is intended to be):

--- /usr/lib/python2.6/site-packages/sqlalchemy/ext/
declarative.py
2010-09-07 19:05:34.0 +0200
+++ lib/python2.6/site-packages/SQLAlchemy-0.6.4-
py2.6.egg/sqlalchemy/ext/declarative.py2010-10-21
19:58:15.863671633
+0200
@@ -936,11 +936,13 @@
 '__table__' in dict_ and
 name in dict_['__table__'].c
 ):
-potential_columns[name] = \
-column_copies[obj] = \
-obj.copy()
-column_copies[obj]._creation_order = \
-obj._creation_order
+if name not in potential_columns:
+potential_columns[name] = \
+column_copies[obj] = \
+obj.copy()
+column_copies[obj]._creation_order =
\
+obj._creation_order
+
 elif isinstance(obj, MapperProperty):
 raise exceptions.InvalidRequestError(
 Mapper properties (i.e. deferred,

---

Oystein

-- 
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] Attribute overriding in mixin inheritance hierarchy

2010-10-21 Thread Michael Bayer

On Oct 21, 2010, at 2:39 PM, oystein wrote:

 Hi all,
 
 I am using declarative to model an existing object hierarchy where
 there is an
 abstract part that i model as an hierarchy of mixins and a concrete
 part where
 i use joined-table inheritance.
 
 I have a problem when there is attributes with the same name, one
 overriding
 the other, in the abstract mixin hierarchy. Maybe I am doing something
 wrong
 so suggestions for how to implement this is most welcome.

Your patch is completely correct in that an attribute on a mixin should take 
precedence that of super-class mixins.The story is a little different for 
attributes on mapped classes, but that's not an issue here.  The patch leaves 
existing tests unaffected, and is committed in r67a7868cfba1 along with a new 
test...thanks !



 
 Sample code:
 
 import unittest
 from sqlalchemy.orm import sessionmaker
 from sqlalchemy import create_engine
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy import Column, String, Integer, Boolean
 
 Base = declarative_base()
 
 class AbstractA(object):
AttrA = Column(String(50))
def __init__(self, AttrA=AttrA):
self.AttrA = AttrA
 
 class AbstractB(AbstractA):
AttrA = Column(String(50), primary_key=True)
def __init__(self, AttrA, **kwargs):
AbstractA.__init__(self, **kwargs)
self.AttrA = AttrA
 
 class Concrete(Base, AbstractB):
__tablename__ = 'Concrete'
AttrB = Column(Boolean)
def __init__(self, AttrB, **kwargs):
AbstractB.__init__(self, **kwargs)
self.AttrB = AttrB
 
 
 class TestAttributeOverriding(unittest.TestCase):
 
def testAttributeOverriding(self):
concrete = Concrete(AttrB = False, AttrA=0)
self.session.add(concrete)
self.session.commit()
assert len(self.session.query(Concrete).all()) == 1
 
def setUp(self):
self.engine = create_engine('sqlite:///:memory:', echo = True)
Session = sessionmaker(bind = self.engine)
self.session = Session()
self.metadata = Base.metadata
self.metadata.create_all(self.engine)
 
def tearDown(self):
self.metadata.drop_all(self.engine)
self.session.close()
 
 
 
 This would fail with could not assemble any primary key columns for
 mapped
 table unless i apply the small patch below, which makes it work as i
 would
 expect (although i don't know if this is how it is intended to be):
 
 --- /usr/lib/python2.6/site-packages/sqlalchemy/ext/
 declarative.py
 2010-09-07 19:05:34.0 +0200
 +++ lib/python2.6/site-packages/SQLAlchemy-0.6.4-
 py2.6.egg/sqlalchemy/ext/declarative.py2010-10-21
 19:58:15.863671633
 +0200
 @@ -936,11 +936,13 @@
 '__table__' in dict_ and
 name in dict_['__table__'].c
 ):
 -potential_columns[name] = \
 -column_copies[obj] = \
 -obj.copy()
 -column_copies[obj]._creation_order = \
 -obj._creation_order
 +if name not in potential_columns:
 +potential_columns[name] = \
 +column_copies[obj] = \
 +obj.copy()
 +column_copies[obj]._creation_order =
 \
 +obj._creation_order
 +
 elif isinstance(obj, MapperProperty):
 raise exceptions.InvalidRequestError(
 Mapper properties (i.e. deferred,
 
 ---
 
 Oystein
 
 -- 
 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] Is it safe to use ORM alongside with the use of mysql queries?

2010-10-21 Thread Michael Bayer
there's no problem at all.   If you are working with the ORM and SQL in one 
transaction, you might need to expire your objects to get a hold of the changes 
you issue via SQL, until you commit() the transaction which expires everything 
(or start a new session).

On Oct 20, 2010, at 3:16 PM, palmprefan wrote:

 Hi,
 
 I am a sqlalchemy newbie and wonder if it is safe to do this in my
 code (for read only):
 
 conn = connection.engine.connect()
 conn.execute(select[t1.amount])
 
 while at the same time in another thread querying/updating t1 via an
 object mapped to the table.
 
 Please let me know if this would cause potential problems.
 
 Thanks in advance.
 
 -- 
 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] How to update TableRelations

2010-10-21 Thread Michael Bayer

On Oct 20, 2010, at 3:38 AM, Aydın ŞEN wrote:

 I defined my tables below as declarative
 
 class MyTable(Base):
 __tablename__ = 'mytable'
 id = Column(Integer,primary_key = True)
 title = Column(String(200))
 description = Column(String(200))
 dt_st = Column(Date, default=func.current_date())
 dt_fn = Column(Date, default=func.current_date())
 content = Column(Unicode)
 bla bla bla
 
 I have a dictionary post data which includes update values, let it be:
 
 myDict = {title: newTitle, content: newContent}
 
 myDict values are arbitrary so i want to update MyTable with only keys and 
 values in myDict (not write one by one table fields). What is the elegant way 
 to do this?

query has an update():

query(MyTable).filter(MyTable.id==5).update(myDict)

otherwise:

for k in myDict:
setattr(some_object, k, myDict[k])
Session.commit()

 
 My second question is about relation, lets add this relation definition to my 
 table definition:
 
 categories = relation(Category, order_by=Category.id, 
 backref=MyTable)
 
 class Category(Base):
 __tablename__ = 'category'
 id = Column(Integer, primary_key = True)
 title = Column(String(200))  
 description = Column(String(200))
 
 How can i update categories when i update MyTable?

same techniques, update myDict with {'categories':[x, y, z]} 


 
 
 -- 
 Aydın Ş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.



Re: [sqlalchemy] dynamic load better choice than joinedload?

2010-10-21 Thread Michael Bayer

On Oct 20, 2010, at 5:54 PM, Alvaro Reinoso wrote:

 Hello,
 
 I'd like to know what the best choice is. This is what I do in my
 system:
 
 I'm using grok server and python on the server side; and javascript on
 the client side. I store all the user data in a session object and
 this data gets called twice, one to render the HTML on the server side
 and another to send the data to the client side because the client
 side is dynamic. I'm using joinedload right know but I know I can't
 use operations such as, append and remove, when the user update or add
 something.

there's no restriction on collection mutation when different loaders are 
called.  The collection has append(), remove(), extend(), __setitem__(), etc.


 
 The timeout of the session object is one hour. I'd like to know if
 it's better to use dynamic load because I guess with dynamic load you
 have an open connection to the database, so it might take many
 resources. The user object might contain a big collections of data.

use dynamic loaders if you want to load only particular slices of the 
collection, instead of the whole thing at once, into memory.  If you can afford 
to have the whole collection in memory, then the usual loaders are better.

-- 
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] Result of a table.update()

2010-10-21 Thread Michael Bayer
an UPDATE statement returns no rows unless RETURNING was used to return columns 
from those rows that were updated.   When an UPDATE or DELETE is emitted, 
result.rowcount contains the number of rows that were matched by the 
statement's criterion.

On Oct 21, 2010, at 2:06 AM, Warwick Prince wrote:

 Hi All
 
 I'm using 0.6.4 under Windoze with MySQL,  Python 2.6.4 and I had code that I 
 thought worked before (0.6.3)  - which appeared to break due to this issue.. 
 I could be wrong on this point so I'll just get to the crux of the matter...
 
 I have a result = table.update(whereClause, values=someValuesDict).execute()
 
 The table is updated correctly, however, the resultproxy object I receive as 
 'result' appears to have no members.   If I fetchone() or fetchall() I simply 
 get a None result.  Is this correct?
 
 If this IS correct, how is the best way to tell if the update was a success?  
  I tried putting bad data in the whereClause and it simply did nothing to the 
 database, but my resultproxy was the same.  No Exceptions raised in either 
 case?
 
 Please enlighten me..
 
 Cheers
 Warwick
 
 
 Warwick Prince 
 Managing Director 
 mobile: +61 411 026 992 
 skype: warwickprince  
  
 phone: +61 7 3102 3730 
 fax:  +61 7 3319 6734 
 web: www.mushroomsys.com 
 
 
 
 -- 
 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] Result of a table.update()

2010-10-21 Thread Warwick Prince
HI Michael

Thanks for the info.  .rowcount was the missing link I needed.  I'll also play 
with RETURNING as well as I thought I had tried that at one stage and saw no 
change in the SQL emitted, so moved on.   I'll let you know.

Cheers
Warwick

 an UPDATE statement returns no rows unless RETURNING was used to return 
 columns from those rows that were updated.   When an UPDATE or DELETE is 
 emitted, result.rowcount contains the number of rows that were matched by the 
 statement's criterion.
 
 On Oct 21, 2010, at 2:06 AM, Warwick Prince wrote:
 
 Hi All
 
 I'm using 0.6.4 under Windoze with MySQL,  Python 2.6.4 and I had code that 
 I thought worked before (0.6.3)  - which appeared to break due to this 
 issue.. I could be wrong on this point so I'll just get to the crux of the 
 matter...
 
 I have a result = table.update(whereClause, values=someValuesDict).execute()
 
 The table is updated correctly, however, the resultproxy object I receive as 
 'result' appears to have no members.   If I fetchone() or fetchall() I 
 simply get a None result.  Is this correct?
 
 If this IS correct, how is the best way to tell if the update was a success? 
   I tried putting bad data in the whereClause and it simply did nothing to 
 the database, but my resultproxy was the same.  No Exceptions raised in 
 either case?
 
 Please enlighten me..
 
 Cheers
 Warwick
 
 
 Warwick Prince 
 Managing Director 
 mobile: +61 411 026 992 
 skype: warwickprince  
  
 phone: +61 7 3102 3730 
 fax:  +61 7 3319 6734 
 web: www.mushroomsys.com 
 
 
 
 -- 
 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.

-- 
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] Attribute overriding in mixin inheritance hierarchy

2010-10-21 Thread Øystein Senneset Haaland
On Thursday 21. October 2010 22.58.35 Michael Bayer wrote:
 On Oct 21, 2010, at 2:39 PM, oystein wrote:
  Hi all,
  
  I am using declarative to model an existing object hierarchy where
  there is an
  abstract part that i model as an hierarchy of mixins and a concrete
  part where
  i use joined-table inheritance.
  
  I have a problem when there is attributes with the same name, one
  overriding
  the other, in the abstract mixin hierarchy. Maybe I am doing something
  wrong
  so suggestions for how to implement this is most welcome.
 
 Your patch is completely correct in that an attribute on a mixin should
 take precedence that of super-class mixins.The story is a little
 different for attributes on mapped classes, but that's not an issue here. 
 The patch leaves existing tests unaffected, and is committed in
 r67a7868cfba1 along with a new test...thanks !
 

Thanks for the quick response and the work you put into sqlalchemy!

Its impressive to see how you keep up with all the questions on the 
mailinglist.

---

Oystein

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