[sqlalchemy] Re: one to many relation, removing and adding list issue

2010-08-26 Thread fma
Any suggestion helping solving this?

On 25 août, 13:02, Martin-Leon Francois francois@gmail.com
wrote:
 Hi,

 I am trying in the same  session to detach an instance from a collection ( 
 one to many)
 flush and commit everything (all is ok) and then attach the removed instance  
 again. unsuccessfully.

 in below code,  last assert fails.
 I don't understand why I am not able to append m2 to o1.to_many collection 
 once removed.

 Any idea? ( I use sa 0.5.6)

 thanx, Francois

 meta = MetaData()
 tb_one = Table(one, meta,
                Column('name',String(50)),
                Column('id',Integer, primary_key=True))

 tb_many = Table(many, meta,
                Column('name',String(50)),
                Column('id',Integer, primary_key=True),
                Column('one_id', Integer, 
 ForeignKey(tb_one.c.id,ondelete='CASCADE'), nullable=False),)

 class One(object):
     def __init__(self, name):
         self.name = name

 class Many(object):
     def __init__(self, name):
         self.name = name

 mapper_one = mapper(One,tb_one)
 mapper_many = mapper(Many, tb_many,
                      properties = dict(
                      to_one = relation(One,uselist=False, 
 backref=backref('to_many', cascade=save-update, merge, delete, 
 delete-orphan),)))

 engine = create_engine()
 Session = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)

 meta.bind = engine
 meta.drop_all(checkfirst=True)
 meta.create_all(checkfirst=True)

 s = Session()
 m1 = Many(M1)
 m2 = Many(M2)
 o1 = One(One)
 o1.to_many.append(m1)
 o1.to_many.append(m2)

 s.add_all([m1,m2,o1])
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 2)

 o1.to_many.remove(m2)
 assert(len(o1.to_many) == 1)
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 1)

 o1.to_many.append(m2)
 assert(len(o1.to_many) == 2)
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 2) #this assert fails why?

 s.close()

-- 
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: TypeDecorator Problem with basic association pattern

2010-08-26 Thread Frank
Hi Michael,

yes, you're right of course. This overlapped with the other issue you
mentioned about the nutrient_weight/weight column name. I didn't
realize this at once. When I  change the column name or the mapping
appropriately I don't need to return None anymore.


Thanks again

Frank

-- 
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] .info dict on Column

2010-08-26 Thread Alessandro Dentella
On Tue, Aug 24, 2010 at 02:48:37PM -0400, Michael Bayer wrote:
  Info option is clearly very handy. At the moment I implemented an image
  field in sqlkit, (that's just a bunch of handler in the gui). In order to do
  that I used a type inherited with no addition, just to understand that that
  field is the path to an Image:
  
   class Image(String): pass
  
  clearly another alternative would be to use info={ 'image': true} or
  similar. Is there some caveat that would make one preferred over the other?
 
 The caveat there is that table reflection, which isn't used in your case,
 would produce VARCHAR and not String, or your custom Image type.  It also
 creates a hard linkage of SQLAlchemy type objects to the behavior of your
 application.  The info approach allows the type and your application's
 interpretation of a field to vary independently.

ok, I switched to using .info and I honestly appreciate it.

am I wrong or Columns created with orm.column_property() don't have .info
attribute? Any chance to add it?

sandro
*:-)


-- 
Sandro Dentella  *:-)
http://sqlkit.argolinux.orgSQLkit home page - PyGTK/python/sqlalchemy

-- 
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: one to many relation, removing and adding list issue

2010-08-26 Thread Martin-Leon Francois
hi,

The problem seems to be a little bit more general, I have a unique class (doing 
nothing) mapped to a unique table.
After opening a session, I create an instance, add it to the session, flush, 
commit.
Everything is ok. -- a row in the table, instance in the identity_map of the 
session

Always in the same session, I delete the instance from the session., flush, 
commit.
Everything is ok. -- no row in the table, no  instance in the identity_map of 
the session

Always the same session, I add again the python instance to the session, flush, 
commit.
??? --  no row in the table, instance in the identity_map of the session

Could you have a look to the code below and help me figure out what I doing 
wrong?
fma

from sqlalchemy import __version__, MetaData, Table, Column, Integer, String, 
create_engine, orm #...@unresolvedimport
from sqlalchemy.orm import mapper #...@unresolvedimport
from sqlalchemy.orm import backref #...@unresolvedimport @UnusedImport

print __version__

meta = MetaData()
tb_one = Table(one, meta,
   Column('name',String(50)),
   Column('id',Integer, primary_key=True))

class One(object):
def __init__(self, name):
self.name = name

def __repr__(self):
return self.name

mapper_one = mapper(One,tb_one)
engine = create_engine('postgres://fma:fma6...@localhost:5432/postgres',
  convert_unicode=True)
Session = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
meta.bind = engine
meta.drop_all(checkfirst=True) 
meta.create_all(checkfirst=True)

s = Session()

o1 = One(One)
s.add(o1)
s.add_all([o1])
s.flush()
s.commit()

print list(s.query(One)), s.identity_map
s.delete(o1)
s.flush()
s.commit()
print list(s.query(One)), s.identity_map

#If I uncomment the following line (workaround) instance is written down to
#the database as I would expect
#o1._sa_instance_state = o1._sa_class_manager._create_instance_state(o1)

s.add(o1)
s.add_all([o1])
s.flush()
s.commit()
print list(s.query(One)), s.identity_map
s.close()

Le 26 août 2010 à 08:42, fma a écrit :

 Any suggestion helping solving this?
 
 On 25 août, 13:02, Martin-Leon Francois francois@gmail.com
 wrote:
 Hi,
 
 I am trying in the same  session to detach an instance from a collection ( 
 one to many)
 flush and commit everything (all is ok) and then attach the removed instance 
  again. unsuccessfully.
 
 in below code,  last assert fails.
 I don't understand why I am not able to append m2 to o1.to_many collection 
 once removed.
 
 Any idea? ( I use sa 0.5.6)
 
 thanx, Francois
 
 meta = MetaData()
 tb_one = Table(one, meta,
Column('name',String(50)),
Column('id',Integer, primary_key=True))
 
 tb_many = Table(many, meta,
Column('name',String(50)),
Column('id',Integer, primary_key=True),
Column('one_id', Integer, 
 ForeignKey(tb_one.c.id,ondelete='CASCADE'), nullable=False),)
 
 class One(object):
 def __init__(self, name):
 self.name = name
 
 class Many(object):
 def __init__(self, name):
 self.name = name
 
 mapper_one = mapper(One,tb_one)
 mapper_many = mapper(Many, tb_many,
  properties = dict(
  to_one = relation(One,uselist=False, 
 backref=backref('to_many', cascade=save-update, merge, delete, 
 delete-orphan),)))
 
 engine = create_engine()
 Session = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
 
 meta.bind = engine
 meta.drop_all(checkfirst=True)
 meta.create_all(checkfirst=True)
 
 s = Session()
 m1 = Many(M1)
 m2 = Many(M2)
 o1 = One(One)
 o1.to_many.append(m1)
 o1.to_many.append(m2)
 
 s.add_all([m1,m2,o1])
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 2)
 
 o1.to_many.remove(m2)
 assert(len(o1.to_many) == 1)
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 1)
 
 o1.to_many.append(m2)
 assert(len(o1.to_many) == 2)
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 2) #this assert fails why?
 
 s.close()
 
 -- 
 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] .info dict on Column

2010-08-26 Thread Michael Bayer

On Aug 26, 2010, at 5:09 AM, Alessandro Dentella wrote:

 On Tue, Aug 24, 2010 at 02:48:37PM -0400, Michael Bayer wrote:
 Info option is clearly very handy. At the moment I implemented an image
 field in sqlkit, (that's just a bunch of handler in the gui). In order to do
 that I used a type inherited with no addition, just to understand that that
 field is the path to an Image:
 
 class Image(String): pass
 
 clearly another alternative would be to use info={ 'image': true} or
 similar. Is there some caveat that would make one preferred over the other?
 
 The caveat there is that table reflection, which isn't used in your case,
 would produce VARCHAR and not String, or your custom Image type.  It also
 creates a hard linkage of SQLAlchemy type objects to the behavior of your
 application.  The info approach allows the type and your application's
 interpretation of a field to vary independently.
 
 ok, I switched to using .info and I honestly appreciate it.
 
 am I wrong or Columns created with orm.column_property() don't have .info
 attribute? Any chance to add it?

dig in to class.attr.property.columns[0] or class.attr.__clause_element__().
 Its probably a good idea for .info to be shared on the class.attr at some 
point.




 
 sandro
 *:-)
 
 
 -- 
 Sandro Dentella  *:-)
 http://sqlkit.argolinux.orgSQLkit home page - PyGTK/python/sqlalchemy
 
 -- 
 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] Re: one to many relation, removing and adding list issue

2010-08-26 Thread fma
Sorry if you receive this twice, I am not sure if it had been
correctly posted.

On 26 août, 14:49, Martin-Leon Francois francois@gmail.com
wrote:
 hi,

 The problem seems to be a little bit more general, I have a unique class 
 (doing nothing) mapped to a unique table.
 After opening a session, I create an instance, add it to the session, flush, 
 commit.
 Everything is ok. -- a row in the table, instance in the identity_map of the 
 session

 Always in the same session, I delete the instance from the session., flush, 
 commit.
 Everything is ok. -- no row in the table, no  instance in the identity_map 
 of the session

 Always the same session, I add again the python instance to the session, 
 flush, commit.
 ??? --  no row in the table, instance in the identity_map of the session

 Could you have a look to the code below and help me figure out what I doing 
 wrong?
 fma

 from sqlalchemy import __version__, MetaData, Table, Column, Integer, String, 
 create_engine, orm #...@unresolvedimport
 from sqlalchemy.orm import mapper #...@unresolvedimport
 from sqlalchemy.orm import backref #...@unresolvedimport @UnusedImport

 print __version__

 meta = MetaData()
 tb_one = Table(one, meta,
                Column('name',String(50)),
                Column('id',Integer, primary_key=True))

 class One(object):
     def __init__(self, name):
         self.name = name

     def __repr__(self):
         return self.name

 mapper_one = mapper(One,tb_one)
 engine = create_engine('postgres://fma:fma6...@localhost:5432/postgres',
                       convert_unicode=True)
 Session = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
 meta.bind = engine
 meta.drop_all(checkfirst=True)
 meta.create_all(checkfirst=True)

 s = Session()

 o1 = One(One)
 s.add(o1)
 s.add_all([o1])
 s.flush()
 s.commit()

 print list(s.query(One)), s.identity_map
 s.delete(o1)
 s.flush()
 s.commit()
 print list(s.query(One)), s.identity_map

 #If I uncomment the following line (workaround) instance is written down to
 #the database as I would expect
 #o1._sa_instance_state = o1._sa_class_manager._create_instance_state(o1)

 s.add(o1)
 s.add_all([o1])
 s.flush()
 s.commit()
 print list(s.query(One)), s.identity_map
 s.close()

 Le 26 août 2010 à 08:42, fma a écrit :



  Any suggestion helping solving this?

  On 25 août, 13:02, Martin-Leon Francois francois@gmail.com
  wrote:
  Hi,

  I am trying in the same  session to detach an instance from a collection ( 
  one to many)
  flush and commit everything (all is ok) and then attach the removed 
  instance  again. unsuccessfully.

  in below code,  last assert fails.
  I don't understand why I am not able to append m2 to o1.to_many collection 
  once removed.

  Any idea? ( I use sa 0.5.6)

  thanx, Francois

  meta = MetaData()
  tb_one = Table(one, meta,
                 Column('name',String(50)),
                 Column('id',Integer, primary_key=True))

  tb_many = Table(many, meta,
                 Column('name',String(50)),
                 Column('id',Integer, primary_key=True),
                 Column('one_id', Integer, 
  ForeignKey(tb_one.c.id,ondelete='CASCADE'), nullable=False),)

  class One(object):
      def __init__(self, name):
          self.name = name

  class Many(object):
      def __init__(self, name):
          self.name = name

  mapper_one = mapper(One,tb_one)
  mapper_many = mapper(Many, tb_many,
                       properties = dict(
                       to_one = relation(One,uselist=False, 
  backref=backref('to_many', cascade=save-update, merge, delete, 
  delete-orphan),)))

  engine = create_engine()
  Session = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)

  meta.bind = engine
  meta.drop_all(checkfirst=True)
  meta.create_all(checkfirst=True)

  s = Session()
  m1 = Many(M1)
  m2 = Many(M2)
  o1 = One(One)
  o1.to_many.append(m1)
  o1.to_many.append(m2)

  s.add_all([m1,m2,o1])
  s.flush()
  s.commit()
  assert(len(o1.to_many) == 2)

  o1.to_many.remove(m2)
  assert(len(o1.to_many) == 1)
  s.flush()
  s.commit()
  assert(len(o1.to_many) == 1)

  o1.to_many.append(m2)
  assert(len(o1.to_many) == 2)
  s.flush()
  s.commit()
  assert(len(o1.to_many) == 2) #this assert fails why?

  s.close()

  --
  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: one to many relation, removing and adding list issue

2010-08-26 Thread Michael Bayer

On Aug 26, 2010, at 8:49 AM, Martin-Leon Francois wrote:

 hi,
 
 The problem seems to be a little bit more general, I have a unique class 
 (doing nothing) mapped to a unique table.
 After opening a session, I create an instance, add it to the session, flush, 
 commit.
 Everything is ok. -- a row in the table, instance in the identity_map of the 
 session
 
 Always in the same session, I delete the instance from the session., flush, 
 commit.
 Everything is ok. -- no row in the table, no  instance in the identity_map 
 of the session
 
 Always the same session, I add again the python instance to the session, 
 flush, commit.
 ??? --  no row in the table, instance in the identity_map of the session
 
 Could you have a look to the code below and help me figure out what I doing 
 wrong?
 fma
 
 #If I uncomment the following line (workaround) instance is written down to
 #the database as I would expect
 #o1._sa_instance_state = o1._sa_class_manager._create_instance_state(o1)
 
 s.add(o1)
 s.add_all([o1])
 s.flush()
 s.commit()
 print list(s.query(One)), s.identity_map
 s.close()

OK this is a little strange.  o1 has been deleted.   You've found one 
particular operation that doesn't immediately raise (add()), but anything else 
you did with o1 would, for example:

print o1.name

you will get this (since o1 is expired and will refresh, then fail):

sqlalchemy.orm.exc.ObjectDeletedError: Instance 'One at 0x1288c70' 
has been deleted.

as you've already figured out, o1 has state on it that tells SQLAlchemy that 
this object is already persistent - no INSERT will be executed again for the 
object.   The strange part here is that the object is beyond persistent and is 
in the deleted state, and we don't have an in-memory flag that would signal 
this, though perhaps that is called for here, I've never seen anyone trying to 
perform such an operation before.

Anyway to convert from persistent back to transient, use make_transient:

from sqlalchemy.orm import make_transient
make_transient(o1)

then the key is removed and the object is as though you just created it.

I want to check if there's some foolproof way we can block on add().

Also, the add()/add_all() calls are redundant as is the flush() right before 
the commit().


 
 Le 26 août 2010 à 08:42, fma a écrit :
 
 Any suggestion helping solving this?
 
 On 25 août, 13:02, Martin-Leon Francois francois@gmail.com
 wrote:
 Hi,
 
 I am trying in the same  session to detach an instance from a collection ( 
 one to many)
 flush and commit everything (all is ok) and then attach the removed 
 instance  again. unsuccessfully.
 
 in below code,  last assert fails.
 I don't understand why I am not able to append m2 to o1.to_many collection 
 once removed.
 
 Any idea? ( I use sa 0.5.6)
 
 thanx, Francois
 
 meta = MetaData()
 tb_one = Table(one, meta,
Column('name',String(50)),
Column('id',Integer, primary_key=True))
 
 tb_many = Table(many, meta,
Column('name',String(50)),
Column('id',Integer, primary_key=True),
Column('one_id', Integer, 
 ForeignKey(tb_one.c.id,ondelete='CASCADE'), nullable=False),)
 
 class One(object):
 def __init__(self, name):
 self.name = name
 
 class Many(object):
 def __init__(self, name):
 self.name = name
 
 mapper_one = mapper(One,tb_one)
 mapper_many = mapper(Many, tb_many,
  properties = dict(
  to_one = relation(One,uselist=False, 
 backref=backref('to_many', cascade=save-update, merge, delete, 
 delete-orphan),)))
 
 engine = create_engine()
 Session = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
 
 meta.bind = engine
 meta.drop_all(checkfirst=True)
 meta.create_all(checkfirst=True)
 
 s = Session()
 m1 = Many(M1)
 m2 = Many(M2)
 o1 = One(One)
 o1.to_many.append(m1)
 o1.to_many.append(m2)
 
 s.add_all([m1,m2,o1])
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 2)
 
 o1.to_many.remove(m2)
 assert(len(o1.to_many) == 1)
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 1)
 
 o1.to_many.append(m2)
 assert(len(o1.to_many) == 2)
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 2) #this assert fails why?
 
 s.close()
 
 -- 
 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 

[sqlalchemy] hot to get connection pool from engine?

2010-08-26 Thread keekychen.shared
 Dear All,

After I read SQLAlchemy;s FAQ I think below code may works,

import sqlalchemy.pool as pool 
import sqlite3 as sqlite3   
conn_proxy = pool.manage(sqlite3)   
# then connect normally 
connection = conn_proxy.connect(...)

however I also get below snippets:

engine = create_engine(...) 
conn = engine.connect() 
conn.connection.do DBAPI things 
cursor = conn.connection.cursor(DBAPI specific arguments..)

then my question is how to get pool from engine? I means can I do
something like this?

conn_proxy = engine.pool.manage(sqlite3)
or
conn_pool = engine.pool

and is there a way can commit every connection in a connection pool?

Thanks!

Rgs,

KC

-- 
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: one to many relation, removing and adding list issue

2010-08-26 Thread Martin-Leon Francois
ok, many thx

fma

Le 26 août 2010 à 17:03, Michael Bayer a écrit :

 
 On Aug 26, 2010, at 8:49 AM, Martin-Leon Francois wrote:
 
 hi,
 
 The problem seems to be a little bit more general, I have a unique class 
 (doing nothing) mapped to a unique table.
 After opening a session, I create an instance, add it to the session, flush, 
 commit.
 Everything is ok. -- a row in the table, instance in the identity_map of 
 the session
 
 Always in the same session, I delete the instance from the session., flush, 
 commit.
 Everything is ok. -- no row in the table, no  instance in the identity_map 
 of the session
 
 Always the same session, I add again the python instance to the session, 
 flush, commit.
 ??? --  no row in the table, instance in the identity_map of the session
 
 Could you have a look to the code below and help me figure out what I doing 
 wrong?
 fma
 
 #If I uncomment the following line (workaround) instance is written down to
 #the database as I would expect
 #o1._sa_instance_state = o1._sa_class_manager._create_instance_state(o1)
 
 s.add(o1)
 s.add_all([o1])
 s.flush()
 s.commit()
 print list(s.query(One)), s.identity_map
 s.close()
 
 OK this is a little strange.  o1 has been deleted.   You've found one 
 particular operation that doesn't immediately raise (add()), but anything 
 else you did with o1 would, for example:
 
   print o1.name
 
 you will get this (since o1 is expired and will refresh, then fail):
 
   sqlalchemy.orm.exc.ObjectDeletedError: Instance 'One at 0x1288c70' 
 has been deleted.
 
 as you've already figured out, o1 has state on it that tells SQLAlchemy that 
 this object is already persistent - no INSERT will be executed again for the 
 object.   The strange part here is that the object is beyond persistent and 
 is in the deleted state, and we don't have an in-memory flag that would 
 signal this, though perhaps that is called for here, I've never seen anyone 
 trying to perform such an operation before.
 
 Anyway to convert from persistent back to transient, use make_transient:
   
   from sqlalchemy.orm import make_transient
   make_transient(o1)
 
 then the key is removed and the object is as though you just created it.
 
 I want to check if there's some foolproof way we can block on add().
 
 Also, the add()/add_all() calls are redundant as is the flush() right before 
 the commit().
 
 
 
 Le 26 août 2010 à 08:42, fma a écrit :
 
 Any suggestion helping solving this?
 
 On 25 août, 13:02, Martin-Leon Francois francois@gmail.com
 wrote:
 Hi,
 
 I am trying in the same  session to detach an instance from a collection ( 
 one to many)
 flush and commit everything (all is ok) and then attach the removed 
 instance  again. unsuccessfully.
 
 in below code,  last assert fails.
 I don't understand why I am not able to append m2 to o1.to_many collection 
 once removed.
 
 Any idea? ( I use sa 0.5.6)
 
 thanx, Francois
 
 meta = MetaData()
 tb_one = Table(one, meta,
Column('name',String(50)),
Column('id',Integer, primary_key=True))
 
 tb_many = Table(many, meta,
Column('name',String(50)),
Column('id',Integer, primary_key=True),
Column('one_id', Integer, 
 ForeignKey(tb_one.c.id,ondelete='CASCADE'), nullable=False),)
 
 class One(object):
 def __init__(self, name):
 self.name = name
 
 class Many(object):
 def __init__(self, name):
 self.name = name
 
 mapper_one = mapper(One,tb_one)
 mapper_many = mapper(Many, tb_many,
  properties = dict(
  to_one = relation(One,uselist=False, 
 backref=backref('to_many', cascade=save-update, merge, delete, 
 delete-orphan),)))
 
 engine = create_engine()
 Session = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
 
 meta.bind = engine
 meta.drop_all(checkfirst=True)
 meta.create_all(checkfirst=True)
 
 s = Session()
 m1 = Many(M1)
 m2 = Many(M2)
 o1 = One(One)
 o1.to_many.append(m1)
 o1.to_many.append(m2)
 
 s.add_all([m1,m2,o1])
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 2)
 
 o1.to_many.remove(m2)
 assert(len(o1.to_many) == 1)
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 1)
 
 o1.to_many.append(m2)
 assert(len(o1.to_many) == 2)
 s.flush()
 s.commit()
 assert(len(o1.to_many) == 2) #this assert fails why?
 
 s.close()
 
 -- 
 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, 

Re: [sqlalchemy] one to many relation, removing and adding list issue

2010-08-26 Thread Michael Bayer

On Aug 25, 2010, at 7:02 AM, Martin-Leon Francois wrote:

 assert(len(o1.to_many) == 2) #this assert fails why?
 

OK so as of r2e09679be06b, your original test now returns:

sqlalchemy.exc.InvalidRequestError: Instance 'Many at 0x12922f0' has been 
deleted.  Use the make_transient() function to send this object back to the 
transient state.


so that it's clear what's going on.

-- 
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] reflected column spec doesn't match model column spec

2010-08-26 Thread Chris Withers

Hi All,

For the following model:

class Header(Base):
__tablename__ = 'header'
id = Column(Integer, primary_key=True)
message_id = Column(Integer,ForeignKey('message.id'))
name = Column(String(50))
value = Column(Text(255))

sqlalchemy-migrate's SchemaDiff tool against the table freshly created 
in MySQL gives:


Schema diffs:
  tables with differences: header
header with different declaration of columns in database: 
[(Column('value', Text(length=255, convert_unicode=False, 
assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), 
table=header), Column(u'value', TINYTEXT(), table=header), 'value 
TEXT(255)', 'value TINYTEXT')]


The schema diff stuff basically does the following to get the database 
column declaration with SA 0.6+:


from sqlalchemy.ext import compiler
from sqlalchemy.schema import DDLElement
class DefineColumn(DDLElement):
def __init__(self, col):
self.col = col

@compiler.compiles(DefineColumn)
def compile(elem, compiler, **kw):
return compiler.get_column_specification(elem.col)

def get_column_specification(col):
return str(DefineColumn(col).compile(dialect=self.conn.dialect))

How can it be changed so that the column reflected from the DB and the 
column calculated from the model end up being the same?


cheers,

Chris

--
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] how to get connection pool from engine?

2010-08-26 Thread keekychen.shared


Sorry for last mail, I clicked send without spell check, so it may cause 
confuse, pls ignore last mail.

=

Dear All,

After I read SQLAlchemy's FAQ I think below code may works,

import sqlalchemy.pool as pool 
import sqlite3 as sqlite3   
conn_proxy = pool.manage(sqlite3)   
# then connect normally 
connection = conn_proxy.connect(...)

however I also get below snippets:

engine = create_engine(...) 
conn = engine.connect() 
conn.connection.do DBAPI things 
cursor = conn.connection.cursor(DBAPI specific arguments..)

then my question is how to get pool from engine? I means can I do
something like this?

conn_proxy = engine.pool.manage(sqlite3)
or
conn_pool = engine.pool

and is there a way can commit every connection in a connection pool?

Thanks!

Rgs,



-- 
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] reflected column spec doesn't match model column spec

2010-08-26 Thread Michael Bayer

On Aug 26, 2010, at 11:42 AM, Chris Withers wrote:

 Hi All,
 
 For the following model:
 
 class Header(Base):
__tablename__ = 'header'
id = Column(Integer, primary_key=True)
message_id = Column(Integer,ForeignKey('message.id'))
name = Column(String(50))
value = Column(Text(255))
 
 sqlalchemy-migrate's SchemaDiff tool against the table freshly created in 
 MySQL gives:
 
 Schema diffs:
  tables with differences: header
header with different declaration of columns in database: 
 [(Column('value', Text(length=255, convert_unicode=False, 
 assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), 
 table=header), Column(u'value', TINYTEXT(), table=header), 'value 
 TEXT(255)', 'value TINYTEXT')]
 
 The schema diff stuff basically does the following to get the database column 
 declaration with SA 0.6+:
 
from sqlalchemy.ext import compiler
from sqlalchemy.schema import DDLElement
class DefineColumn(DDLElement):
def __init__(self, col):
self.col = col
 
@compiler.compiles(DefineColumn)
def compile(elem, compiler, **kw):
return compiler.get_column_specification(elem.col)
 
def get_column_specification(col):
return str(DefineColumn(col).compile(dialect=self.conn.dialect))
 
 How can it be changed so that the column reflected from the DB and the column 
 calculated from the model end up being the same?

if schemas are being diffed, types can be compared generically using type 
affinity.  This is described at:

http://www.sqlalchemy.org/trac/wiki/06Migration#ReflectionReturnsDialect-SpecificTypes

You could also take a reflected table and force all of its types down to the 
generic ones using a hack like column.type.__class__ = 
column.type._type_affinity to change the class of each type down to its basic 
version. But if a diff is all that's needed they should be using that.

(FYI alembic will have no schema diff feature its not something I buy into much)

-- 
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] CircularDependencyError on 0.6 (works on 0.5.8)

2010-08-26 Thread Alessandro Dentella
Hi again, sorry for flooding with email this week...

I stumbled on the CircularDependencyError in some occasions with self
referencing models. I do understand that it can be tricky to INSERT and DELETE
but I'm just updating rows.

I reduced my problem to the bare minimum. It works both on 0.5.8 and 0.6.3
in the test case, but as long as I use the same objects from a graphical GUI
I get an error, Debugging with pdb, the problem arises on session.begin()
and in that moment the situation seems to me exactly the one of the test (2
objects, one modified).

I hope the error message is meaningful to you...

So the situation:

   from sqlalchemy.ext.declarative import declarative_base
   from sqlalchemy import Table, Column, ForeignKey
   from sqlalchemy.types import *
   from sqlalchemy import orm, sql

   Base = declarative_base()
   URL = 'postgresql://localhost/scuole'
   Base.metadata.bind = URL
   Session = orm.sessionmaker()
   sess = Session(bind=Base.metadata.bind, expire_on_commit=False, 
autoflush=False, autocommit=True, ) 

   class School(Base):
   __tablename__ = 'scuola_scuola'

   cod = Column(String(10), primary_key=True)
   denominazione= Column(String(120))
   cod_riferimento = Column(String(10), ForeignKey(cod))
   cliente = Column(Boolean, nullable=False)

   sedi = orm.relation('School', )

   def __repr__(self):
return self.cod

   Base.metadata.create_all()

   # s1 = School(cod=S1, cod_riferimento=S1, cliente=False)
   # d1 = School(cod=D1, cod_riferimento=S1, cliente=False)
   # sess.add(s1)
   # sess.add(d1)
   # sess.commit()
   s1 = sess.query(School).get('S1')
   d1 = sess.query(School).get('D1')
   d1.cliente = False
   sess.begin()
   sess.commit()

This same peace of code (i.e.: same session with just s1, d1), run from
within a GUI raises an error (only with SA 0.6.3, 0.5.8 just works). The
error is:



Traceback (most recent call last):
  File /misc/src/hg/py/sqlkit/sqlkit/widgets/mask/mask.py, line 388, in 
record_save_cb
self.record_save(None)
  File /misc/src/hg/py/sqlkit/sqlkit/widgets/mask/mask.py, line 422, in 
record_save
self.commit()
  File /misc/src/hg/py/sqlkit/sqlkit/widgets/common/sqlwidget.py, line 972, 
in commit
self.session.begin()
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
598, in begin
self, nested=nested)
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
223, in __init__
self._take_snapshot()
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
271, in _take_snapshot
self.session.flush()
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
1346, in flush
self._flush(objects)
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
1427, in _flush
flush_context.execute()
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py, line 
291, in execute
postsort_actions):
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/topological.py, line 
31, in sort_as_subsets
(find_cycles(tuples, allitems), _dump_edges(edges, True)))

sqlalchemy.exc.CircularDependencyError: Circular dependency detected:
  cycles: set([SaveUpdateState(School at 0x8da616c),
  ProcessState(OneToManyDP(School.sedi), School at 0x8da616c,
  delete=False)]) all edges: [(SaveUpdateState(School at 0x8da616c),
  ProcessState(OneToManyDP(School.sedi), School at 0x8da616c,
  delete=False)), (ProcessState(OneToManyDP(School.sedi), School at
  0x8da616c, delete=False), SaveUpdateState(School at 0x8da616c)),
  (SaveUpdateState(School at 0x8da616c), SaveUpdateState(School at
  0x8da616c)), (SaveUpdateState(School at 0x8da616c),
  SaveUpdateState(School at 0x8da61ec)),
  (ProcessState(OneToManyDP(School.sedi), School at 0x8da616c,
  delete=False), SaveUpdateState(School at 0x8da61ec))]

How can I further investigate what Is wrong from the setup of my GUI?

Thanks again
sandro
*:-)


-- 
Sandro Dentella  *:-)
http://sqlkit.argolinux.orgSQLkit home page - PyGTK/python/sqlalchemy

-- 
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] reflected column spec doesn't match model column spec

2010-08-26 Thread Chris Withers

Michael Bayer wrote:

How can it be changed so that the column reflected from the DB and the column 
calculated from the model end up being the same?


if schemas are being diffed, types can be compared generically using type 
affinity.  This is described at:

http://www.sqlalchemy.org/trac/wiki/06Migration#ReflectionReturnsDialect-SpecificTypes

You could also take a reflected table and force all of its types down to the generic 
ones using a hack like column.type.__class__ = column.type._type_affinity to change the class of 
each type down to its basic version. But if a diff is all that's needed they should 
be using that.


Cool, I shall have a look into that, thanks!


(FYI alembic will have no schema diff feature its not something I buy into much)


Unfortunately, I'm in an environment where people are accident prone / 
lazy. Numbers in a version table do not imply the schema actually 
matches the model ;-)


Schema diffing is the only way I can see to be safe.

How's Alembic progressing? Some of the stuff in Migrate (like the monkey 
patching of new bases classes into things like Table and Column) makes 
my skin crawl, but I didn't see any checkins to Alembic in the last few 
months...


Chris

--
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] CircularDependencyError on 0.6 (works on 0.5.8)

2010-08-26 Thread Michael Bayer
need a full test case.  Here's yours, runs fine:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.types import *
from sqlalchemy.orm import *

Base = declarative_base()
e = create_engine('sqlite://', echo=True)
Base.metadata.bind = e

sess = sessionmaker(e)()

class School(Base):
__tablename__ = 'scuola_scuola'

cod = Column(String(10), primary_key=True)
denominazione= Column(String(120))
cod_riferimento = Column(String(10), ForeignKey(cod))
cliente = Column(Boolean, nullable=False)

sedi = relation('School', )

def __repr__(self):
return self.cod

Base.metadata.create_all()

s1 = School(cod=S1, cod_riferimento=S1, cliente=False)
d1 = School(cod=D1, cod_riferimento=S1, cliente=False)
sess.add(s1)
sess.add(d1)
sess.commit()
s1 = sess.query(School).get('S1')
d1 = sess.query(School).get('D1')
d1.cliente = False
sess.commit()


On Aug 26, 2010, at 12:12 PM, Alessandro Dentella wrote:

 Hi again, sorry for flooding with email this week...
 
 I stumbled on the CircularDependencyError in some occasions with self
 referencing models. I do understand that it can be tricky to INSERT and DELETE
 but I'm just updating rows.
 
 I reduced my problem to the bare minimum. It works both on 0.5.8 and 0.6.3
 in the test case, but as long as I use the same objects from a graphical GUI
 I get an error, Debugging with pdb, the problem arises on session.begin()
 and in that moment the situation seems to me exactly the one of the test (2
 objects, one modified).
 
 I hope the error message is meaningful to you...
 
 So the situation:
 
   from sqlalchemy.ext.declarative import declarative_base
   from sqlalchemy import Table, Column, ForeignKey
   from sqlalchemy.types import *
   from sqlalchemy import orm, sql
 
   Base = declarative_base()
   URL = 'postgresql://localhost/scuole'
   Base.metadata.bind = URL
   Session = orm.sessionmaker()
   sess = Session(bind=Base.metadata.bind, expire_on_commit=False, 
 autoflush=False, autocommit=True, ) 
 
   class School(Base):
   __tablename__ = 'scuola_scuola'
 
   cod = Column(String(10), primary_key=True)
   denominazione= Column(String(120))
   cod_riferimento = Column(String(10), ForeignKey(cod))
   cliente = Column(Boolean, nullable=False)
 
   sedi = orm.relation('School', )
 
   def __repr__(self):
   return self.cod
 
   Base.metadata.create_all()
 
   # s1 = School(cod=S1, cod_riferimento=S1, cliente=False)
   # d1 = School(cod=D1, cod_riferimento=S1, cliente=False)
   # sess.add(s1)
   # sess.add(d1)
   # sess.commit()
   s1 = sess.query(School).get('S1')
   d1 = sess.query(School).get('D1')
   d1.cliente = False
   sess.begin()
   sess.commit()
 
 This same peace of code (i.e.: same session with just s1, d1), run from
 within a GUI raises an error (only with SA 0.6.3, 0.5.8 just works). The
 error is:
 
 
 
 Traceback (most recent call last):
  File /misc/src/hg/py/sqlkit/sqlkit/widgets/mask/mask.py, line 388, in 
 record_save_cb
self.record_save(None)
  File /misc/src/hg/py/sqlkit/sqlkit/widgets/mask/mask.py, line 422, in 
 record_save
self.commit()
  File /misc/src/hg/py/sqlkit/sqlkit/widgets/common/sqlwidget.py, line 972, 
 in commit
self.session.begin()
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 598, in begin
self, nested=nested)
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 223, in __init__
self._take_snapshot()
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 271, in _take_snapshot
self.session.flush()
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 1346, in flush
self._flush(objects)
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 1427, in _flush
flush_context.execute()
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py, 
 line 291, in execute
postsort_actions):
  File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/topological.py, line 
 31, in sort_as_subsets
(find_cycles(tuples, allitems), _dump_edges(edges, True)))
 
 sqlalchemy.exc.CircularDependencyError: Circular dependency detected:
  cycles: set([SaveUpdateState(School at 0x8da616c),
  ProcessState(OneToManyDP(School.sedi), School at 0x8da616c,
  delete=False)]) all edges: [(SaveUpdateState(School at 0x8da616c),
  ProcessState(OneToManyDP(School.sedi), School at 0x8da616c,
  delete=False)), (ProcessState(OneToManyDP(School.sedi), School at
  0x8da616c, delete=False), SaveUpdateState(School at 0x8da616c)),
  (SaveUpdateState(School at 0x8da616c), SaveUpdateState(School at
  0x8da616c)), (SaveUpdateState(School at 0x8da616c),
  SaveUpdateState(School at 0x8da61ec)),
  (ProcessState(OneToManyDP(School.sedi), School at 0x8da616c,
  delete=False), SaveUpdateState(School at 0x8da61ec))]
 
 How can I further investigate what Is wrong from the setup of my GUI?
 
 Thanks 

Re: [sqlalchemy] CircularDependencyError on 0.6 (works on 0.5.8)

2010-08-26 Thread Michael Bayer
of course you'll get a cycle if you do this, though:

s1 = School(cod=S1, cod_riferimento=S1, cliente=False)
d1 = School(cod=D1, cod_riferimento=S1, cliente=False)
s1.sedi = [s1, d1]

s1-s1 is not supported by self referential flushes, unless you put 
post_update=True on the relation() you have there.


On Aug 26, 2010, at 12:30 PM, Michael Bayer wrote:

 need a full test case.  Here's yours, runs fine:
 
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy import *
 from sqlalchemy.types import *
 from sqlalchemy.orm import *
 
 Base = declarative_base()
 e = create_engine('sqlite://', echo=True)
 Base.metadata.bind = e
 
 sess = sessionmaker(e)()
 
 class School(Base):
__tablename__ = 'scuola_scuola'
 
cod = Column(String(10), primary_key=True)
denominazione= Column(String(120))
cod_riferimento = Column(String(10), ForeignKey(cod))
cliente = Column(Boolean, nullable=False)
 
sedi = relation('School', )
 
def __repr__(self):
return self.cod
 
 Base.metadata.create_all()
 
 s1 = School(cod=S1, cod_riferimento=S1, cliente=False)
 d1 = School(cod=D1, cod_riferimento=S1, cliente=False)
 sess.add(s1)
 sess.add(d1)
 sess.commit()
 s1 = sess.query(School).get('S1')
 d1 = sess.query(School).get('D1')
 d1.cliente = False
 sess.commit()
 
 
 On Aug 26, 2010, at 12:12 PM, Alessandro Dentella wrote:
 
 Hi again, sorry for flooding with email this week...
 
 I stumbled on the CircularDependencyError in some occasions with self
 referencing models. I do understand that it can be tricky to INSERT and 
 DELETE
 but I'm just updating rows.
 
 I reduced my problem to the bare minimum. It works both on 0.5.8 and 0.6.3
 in the test case, but as long as I use the same objects from a graphical GUI
 I get an error, Debugging with pdb, the problem arises on session.begin()
 and in that moment the situation seems to me exactly the one of the test (2
 objects, one modified).
 
 I hope the error message is meaningful to you...
 
 So the situation:
 
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy import Table, Column, ForeignKey
  from sqlalchemy.types import *
  from sqlalchemy import orm, sql
 
  Base = declarative_base()
  URL = 'postgresql://localhost/scuole'
  Base.metadata.bind = URL
  Session = orm.sessionmaker()
  sess = Session(bind=Base.metadata.bind, expire_on_commit=False, 
 autoflush=False, autocommit=True, ) 
 
  class School(Base):
  __tablename__ = 'scuola_scuola'
 
  cod = Column(String(10), primary_key=True)
  denominazione= Column(String(120))
  cod_riferimento = Column(String(10), ForeignKey(cod))
  cliente = Column(Boolean, nullable=False)
 
  sedi = orm.relation('School', )
 
  def __repr__(self):
  return self.cod
 
  Base.metadata.create_all()
 
  # s1 = School(cod=S1, cod_riferimento=S1, cliente=False)
  # d1 = School(cod=D1, cod_riferimento=S1, cliente=False)
  # sess.add(s1)
  # sess.add(d1)
  # sess.commit()
  s1 = sess.query(School).get('S1')
  d1 = sess.query(School).get('D1')
  d1.cliente = False
  sess.begin()
  sess.commit()
 
 This same peace of code (i.e.: same session with just s1, d1), run from
 within a GUI raises an error (only with SA 0.6.3, 0.5.8 just works). The
 error is:
 
 
 
 Traceback (most recent call last):
 File /misc/src/hg/py/sqlkit/sqlkit/widgets/mask/mask.py, line 388, in 
 record_save_cb
   self.record_save(None)
 File /misc/src/hg/py/sqlkit/sqlkit/widgets/mask/mask.py, line 422, in 
 record_save
   self.commit()
 File /misc/src/hg/py/sqlkit/sqlkit/widgets/common/sqlwidget.py, line 972, 
 in commit
   self.session.begin()
 File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 598, in begin
   self, nested=nested)
 File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 223, in __init__
   self._take_snapshot()
 File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 271, in _take_snapshot
   self.session.flush()
 File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 1346, in flush
   self._flush(objects)
 File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/session.py, line 
 1427, in _flush
   flush_context.execute()
 File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py, 
 line 291, in execute
   postsort_actions):
 File /misc/src/sqlalchemy/sqlalchemy/lib/sqlalchemy/topological.py, line 
 31, in sort_as_subsets
   (find_cycles(tuples, allitems), _dump_edges(edges, True)))
 
 sqlalchemy.exc.CircularDependencyError: Circular dependency detected:
 cycles: set([SaveUpdateState(School at 0x8da616c),
 ProcessState(OneToManyDP(School.sedi), School at 0x8da616c,
 delete=False)]) all edges: [(SaveUpdateState(School at 0x8da616c),
 ProcessState(OneToManyDP(School.sedi), School at 0x8da616c,
 delete=False)), (ProcessState(OneToManyDP(School.sedi), School at
 0x8da616c, delete=False), SaveUpdateState(School at 0x8da616c)),
 (SaveUpdateState(School at 

Re: [sqlalchemy] reflected column spec doesn't match model column spec

2010-08-26 Thread Michael Bayer

On Aug 26, 2010, at 12:15 PM, Chris Withers wrote:

 Michael Bayer wrote:
 How can it be changed so that the column reflected from the DB and the 
 column calculated from the model end up being the same?
 if schemas are being diffed, types can be compared generically using type 
 affinity.  This is described at:
 http://www.sqlalchemy.org/trac/wiki/06Migration#ReflectionReturnsDialect-SpecificTypes
 You could also take a reflected table and force all of its types down to the 
 generic ones using a hack like column.type.__class__ = 
 column.type._type_affinity to change the class of each type down to its 
 basic version. But if a diff is all that's needed they should be using 
 that.
 
 Cool, I shall have a look into that, thanks!
 
 (FYI alembic will have no schema diff feature its not something I buy into 
 much)
 
 Unfortunately, I'm in an environment where people are accident prone / lazy. 
 Numbers in a version table do not imply the schema actually matches the model 
 ;-)

then you have an organizational issue, not a software one.   If you're using 
migrations, people shouldn't be touching the database directly.Databases 
have great, granular security models to ensure that only one user can actually 
ALTER anything.

 
 Schema diffing is the only way I can see to be safe.

DB permissions


 
 How's Alembic progressing? Some of the stuff in Migrate (like the monkey 
 patching of new bases classes into things like Table and Column) makes my 
 skin crawl, but I didn't see any checkins to Alembic in the last few months...

ive got no time to work on it and no need for it at the moment.   There are two 
major 0.7 features that are higher on my radar as well as some major doc 
rewrites.

-- 
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] Change echo at will

2010-08-26 Thread Michael Hipp
Is there a way to set 'echo' at any time? Everything I can find sets it when 
the engine is created and doesn't seem to change it afterward.


Thanks,
Michael

--
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 relationships with a postgres schema using declarative mapping

2010-08-26 Thread Nigel
I think where I'm getting bogged down with the case I have (most
likely because I'm new to relational mapping of db tables anyway) is
how I get data into the intermediate table.  What I'm doing here is
setting up a dependency graph where that entityConnections table
needs to have data explicitly added to it without necessarily having
the relational objects for the entity table around.  Its
relationship with the Entity object is simply to ask what the
upstream and downstream dependencies are.  Maybe the association
object pattern is the way to go with it.

thanks,
nigel



On Aug 25, 6:12 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Aug 25, 2010, at 7:43 PM, Nigel wrote:

  Thanks very much for the quick reply.  The second suggested worked
  nicely.  I guess this explains why I was having such a hard time
  tracking down a similar situation online!

  Maybe I can sneak a second question in here which is to ask why the
  many - many connection table can not be created using the declarative
  method?  Would it be normal to create the mapping for that table after
  it has been created in order to get a relational object?

 A declarative mapping defines a Table, a user-defined class, and a mapper 
 associating the two all at once.  In the case of secondary, that is a Table 
 that is referenced by a relationship() as an intermediary table to satisfy 
 the linkage between two mappers, but is not mapped itself, has no 
 corresponding user defined class, and is not subject to the same persistence 
 methods as that of mapped classes.    

 There is another pattern called the association object pattern, which 
 removes the secondary argument and explicitly maps the intermediary table, 
 and then specifies a relationship() from parent to intermediary class, then 
 another from intermediary to child class.   This is a very common pattern and 
 is used when additional meaningful information is present on the intermediary 
 table, and there is also a helper called the associationproxy which can 
 allow such a configuration to act like a many-to-many when that's all that's 
 needed.

 Both patterns can also be combined.   Doing so is not really necessary as the 
 associationproxy is a cleaner solution, but the secondary method offers a 
 slight performance gain when loading related rows.   If the two patterns are 
 combined, you'd usually want to add viewonly=True to the relationship() 
 that references the Table via secondary.   If this is not done, a mutation 
 to the secondary relationship that is mirrored by mutations to the 
 association relationship can cause conflicting instructions during a flush. 
  



  Thanks again,
  nigel

  On Aug 25, 4:20 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  On Aug 25, 2010, at 4:57 PM, Nigel wrote:

  Hi all,

  I'm having some trouble with using a schema with a many-to-many
  declarative mapping.  The error i'm getting is as follows:

  
  sqlalchemy.exc.InvalidRequestError: One or more mappers failed to
  compile. Exception was probably suppressed within a hasattr() call.
  Message was: One or more mappers failed to compile. Exception was
  probably suppressed within a hasattr() call. Message was: When
  compiling mapper Mapper|Entity|entity, expression 'Entity.id ==
  entityConnections.c.masterId' failed to locate a name (name
  'entityConnections' is not defined). If this is a class name,
  consider adding this relationship() to the class '__main__.Entity'
  class after both dependent classes have been defined.
  

  Here is a bit of code that will fail. Please excuse the word wrapping:

  
  Base = declarative_base( bind = engine )

  entityConnection = Table( 'entityConnections', Base.metadata,
  Column( 'masterId', Integer,
  ForeignKey( 'relationalSchema.entity.id' ), primary_key = True ),
  Column( 'slaveId', Integer,
  ForeignKey( 'relationalSchema.entity.id' ), primary_key = True ),
  schema = 'relationalSchema' )

  class Entity( Base ):
     __tablename__ = 'entity'
     __table_args__ = { 'schema':'relationalSchema' }

     id = Column( Integer, primary_key = True )
     entityType = Column(String)

     connections = relationship( 'Entity', secondary = entityConnection,
  primaryjoin = Entity.id == entityConnections.c.masterId,
  secondaryjoin = Entity.id == entityConnections.c.slaveId,backref =
  'entity' )

     def __init__(self, entityType ):
             self.entityType = entityType

     def __repr__(self):
             return Entity('%s') % ( self.entityType )

  from sqlalchemy.orm import sessionmaker
  Session = sessionmaker(bind=engine)
  session = Session()
  session.add( Entity( 'test' ) )
  

  If I remove the references to the schema the code runs fine.  Adding
  the schema to the primaryjoin and secondaryjoin just changes the error
  so it must be something else.  Any ideas what I'm doing wrong here?
  I'm a rank newbie with sqlalchemy so it's probably a pretty easy thing
  to fix!

  er OK you're actually doing something that has not 

Re: [sqlalchemy] Change echo at will

2010-08-26 Thread Mike Conley
On Thu, Aug 26, 2010 at 4:21 PM, Michael Hipp mich...@hipp.com wrote:

 Is there a way to set 'echo' at any time? Everything I can find sets it
 when the engine is created and doesn't seem to change it afterward.

 Thanks,
 Michael


You can assign the engine.echo property to True or False any time after
creating the engine.

-- 
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 do housekeeping jobs before using sqlalchemy and coworking with python sqlite3 and sqlalchemy ?

2010-08-26 Thread Mike Conley
On Wed, Aug 25, 2010 at 1:30 PM, keekychen.shared 
keekychen.sha...@gmail.com wrote:


 How to test if an existing database file is a valid sqlite3 format
 file before using sqlalchemy?


Here is function we use

import os, os.path as osp
try:
from pysqlite2 import dbapi2 as sqlite
except:
import sqlite3 as sqlite

def isSQLite(filename):
True if filename is a SQLite database
File is database if: (1) file exists, (2) length is non-zero,
(3) can connect, (4) has sqlite_master table

# validate file exists
if not osp.isfile(filename):
return False
# is not an empty file
if not os.stat(filename).st_size:
return False
# can open a connection
try:
conn = sqlite.connect(filename)
except:
return False
# has sqlite_master
try:
result = conn.execute('pragma table_info(sqlite_master)').fetchall()
if len(result) == 0:
conn.close()
return False
except:
conn.close()
return False

# looks like a good database
conn.close()
return True

-- 
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] self referencing column

2010-08-26 Thread waugust
I've been looking through the documentation and I could have sworn I
saw it before but It seem I can't find it once more...
I'm looking for how to set up a self referencing column like in this
pseudo scenerio:

class Action(DeclarativeBase):

   __tablename__ = 'action'

   id = Column(Integer, autoincrement=True, primary_key=True)
onsuccess = Column(Integer, ForeignKey=('action.id')  

I would want to have an id of an action upon the success of an
action...

-- 
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: self referencing column

2010-08-26 Thread waugust
I'm guessing that the answer would be as at:
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DeclarativeSelfReferencingTable

?

On Aug 26, 8:02 pm, waugust waugustyn...@gmail.com wrote:
 I've been looking through the documentation and I could have sworn I
 saw it before but It seem I can't find it once more...
 I'm looking for how to set up a self referencing column like in this
 pseudo scenerio:

 class Action(DeclarativeBase):

            __tablename__ = 'action'

            id = Column(Integer, autoincrement=True, primary_key=True)

     onsuccess = Column(Integer, ForeignKey=('action.id')  

 I would want to have an id of an action upon the success of an
 action...

-- 
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: self referencing column

2010-08-26 Thread Alexandre Conrad
This is the documentation for self referential mapper:

http://www.sqlalchemy.org/docs/mappers.html#adjacency-list-relationships

I guess you have found the recipes for doing it the declarative way.

2010/8/26 waugust waugustyn...@gmail.com:
 I'm guessing that the answer would be as at:
 http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DeclarativeSelfReferencingTable

 ?

 On Aug 26, 8:02 pm, waugust waugustyn...@gmail.com wrote:
 I've been looking through the documentation and I could have sworn I
 saw it before but It seem I can't find it once more...
 I'm looking for how to set up a self referencing column like in this
 pseudo scenerio:

 class Action(DeclarativeBase):

            __tablename__ = 'action'

            id = Column(Integer, autoincrement=True, primary_key=True)

     onsuccess = Column(Integer, ForeignKey=('action.id')  

 I would want to have an id of an action upon the success of an
 action...

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





-- 
Alex
twitter.com/alexconrad

-- 
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: self referencing column

2010-08-26 Thread Wojtek Augustynski
Thanks, though! :)

Don't suppose you would have an idea on how I could store a procedure? :)
the actions to which I'm pertaining I envisioned as controller actions
(Pylons) that I could define an associate with an execution flow... (don't
know if that makes sense)...
I was thinking of just storing the Route url and execute it with app.get or
app.post though idk...

On Thu, Aug 26, 2010 at 10:21 PM, Alexandre Conrad 
alexandre.con...@gmail.com wrote:

 This is the documentation for self referential mapper:

 http://www.sqlalchemy.org/docs/mappers.html#adjacency-list-relationships

 I guess you have found the recipes for doing it the declarative way.

 2010/8/26 waugust waugustyn...@gmail.com:
  I'm guessing that the answer would be as at:
 
 http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DeclarativeSelfReferencingTable
 
  ?
 
  On Aug 26, 8:02 pm, waugust waugustyn...@gmail.com wrote:
  I've been looking through the documentation and I could have sworn I
  saw it before but It seem I can't find it once more...
  I'm looking for how to set up a self referencing column like in this
  pseudo scenerio:
 
  class Action(DeclarativeBase):
 
 __tablename__ = 'action'
 
 id = Column(Integer, autoincrement=True, primary_key=True)
 
  onsuccess = Column(Integer, ForeignKey=('action.id')  
 
  I would want to have an id of an action upon the success of an
  action...
 
  --
  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.comsqlalchemy%2bunsubscr...@googlegroups.com
 .
  For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.
 
 



 --
 Alex
 twitter.com/alexconrad

 --
 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.comsqlalchemy%2bunsubscr...@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.