[sqlalchemy] Load parent on child deletion

2012-03-21 Thread Pau Tallada
Hi!

I have a model with a parent-child relationship on the same entity.
The children collection on a parent instance has events set to intercept
adding and removing instances to/from it. This is needed to update an
attribute from the parent instance.
But when I delete an instance which is the child of another one, that
parent instance is not notified of its child removal and so the event is
not fired.

I tried working with the cascade rules but I have not been successful.

Is this behaviour supported?

Thanks!

Pau.
-- 
--
Pau Tallada Crespí
Dep. d'Astrofísica i Cosmologia
Port d'Informació Científica (PIC)
Tel: +34 93 586 8233
--

-- 
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] with_lockmode('share') ?

2012-03-21 Thread Julien Cigar

Hello,

I'm using SQLAlchemy 0.7.6 and PostgreSQL 9.0.7 which support a FOR 
UPDATE and a FOR SHARE clause to lock selected rows.
I noticed that the .with_lockmode() method of the Query object 
(http://docs.sqlalchemy.org/en/latest/orm/query.html?highlight=with_lock#sqlalchemy.orm.query.Query.with_lockmode) 
only accepts 'update' for PostgreSQL. Could it be an oblivion .. ?


(I tried with the .with_lockmode('read'), but it also generates a FOR 
UPDATE on PostgreSQL)


Thank you,
Julien

--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

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

attachment: jcigar.vcf

[sqlalchemy] Single table inheritance

2012-03-21 Thread Kent
Hoping for advice:  I'm using sqlalchemy against a legacy application's 
database design, most of which isn't in my control.  I have a situation 
where single table inheritance should work beautifully but there is one 
catch: of the 7 polymorphic sub classes, there is one which is allowed to 
change into another.  The rest are immutable.  As an example, suppose a 
Employee were allowed to be promoted to Manager.  

The docs state that the polymorphic_identity is a read only attribute and 
that Behavior is undefined if directly modified.  

I could work around this by mapping 2 polymorphic_identities to one class, 
but as far a I can see, this can only be a single scalar value.  The docs 
say that polymorphic_on may also be of other types besides Column in a 
future SQLAlchemy release... I wonder if I can help sqla map either of 2 
values to a class in this way?

Any advice?

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/vU1nLi3v8sEJ.
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.



Re: [sqlalchemy] Single table inheritance

2012-03-21 Thread Michael Bayer

On Mar 21, 2012, at 9:49 AM, Kent wrote:

 Hoping for advice:  I'm using sqlalchemy against a legacy application's 
 database design, most of which isn't in my control.  I have a situation where 
 single table inheritance should work beautifully but there is one catch: of 
 the 7 polymorphic sub classes, there is one which is allowed to change into 
 another.  The rest are immutable.  As an example, suppose a Employee were 
 allowed to be promoted to Manager.  
 
 The docs state that the polymorphic_identity is a read only attribute and 
 that Behavior is undefined if directly modified.  
 
 I could work around this by mapping 2 polymorphic_identities to one class, 
 but as far a I can see, this can only be a single scalar value.  The docs say 
 that polymorphic_on may also be of other types besides Column in a future 
 SQLAlchemy release... I wonder if I can help sqla map either of 2 values to a 
 class in this way?
 
 Any advice?

did you try just saying employee.type = 'manager', flushing, then loading a new 
copy of it ?  

also polymorphic_on can be any SQL expression in 0.7, like a CASE statement if 
you wanted.

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



Re: [sqlalchemy] with_lockmode('share') ?

2012-03-21 Thread Michael Bayer
the PG dialect has only FOR UPDATE and FOR UPDATE NOWAIT at the moment, 
easy enough to add more options though it would be helpful if someone could 
volunteer a patch on it (with tests as always!):

http://www.sqlalchemy.org/trac/ticket/2445


On Mar 21, 2012, at 6:00 AM, Julien Cigar wrote:

 Hello,
 
 I'm using SQLAlchemy 0.7.6 and PostgreSQL 9.0.7 which support a FOR UPDATE 
 and a FOR SHARE clause to lock selected rows.
 I noticed that the .with_lockmode() method of the Query object 
 (http://docs.sqlalchemy.org/en/latest/orm/query.html?highlight=with_lock#sqlalchemy.orm.query.Query.with_lockmode)
  only accepts 'update' for PostgreSQL. Could it be an oblivion .. ?
 
 (I tried with the .with_lockmode('read'), but it also generates a FOR UPDATE 
 on PostgreSQL)
 
 Thank you,
 Julien
 
 -- 
 No trees were killed in the creation of this message.
 However, many electrons were terribly inconvenienced.
 
 -- 
 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.
 
 jcigar.vcf

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



Re: [sqlalchemy] Load parent on child deletion

2012-03-21 Thread Michael Bayer

On Mar 21, 2012, at 5:18 AM, Pau Tallada wrote:

 Hi!
 
 I have a model with a parent-child relationship on the same entity.
 The children collection on a parent instance has events set to intercept 
 adding and removing instances to/from it. This is needed to update an 
 attribute from the parent instance.
 But when I delete an instance which is the child of another one, that parent 
 instance is not notified of its child removal and so the event is not fired.
 
 I tried working with the cascade rules but I have not been successful.
 
 Is this behaviour supported?

delete an instance, meaning,

parent.children.append(child)
session.delete(child)
session.commit()

?


in that case no, parent.children is not notified of a collection removal, 
since no collection alteration actually takes place.   The child is still 
there in parent.collection until the whole of parent is expired, which occurs 
after the commit().  See 
http://docs.sqlalchemy.org/en/latest/orm/session.html#deleting-from-collections 
for detailed discussion on this.

Your two options here are to stick to collection mutations, or do an additional 
before_flush() or after_flush() event that goes through child objects marked 
deleted, finds their parents, and does what is needed.



 
 Thanks!
 
 Pau.
 -- 
 --
 Pau Tallada Crespí
 Dep. d'Astrofísica i Cosmologia
 Port d'Informació Científica (PIC)
 Tel: +34 93 586 8233
 --
 
 
 
 -- 
 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.

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



Re: [sqlalchemy] with_lockmode('share') ?

2012-03-21 Thread Julien Cigar

Hi Mike,

I would be happy to provide a patch (when I find some time).
I never made any patch for SQLAlchemy, what's the preferred method to do 
it? Attach an `hg diff` to the ticket or a pull request on Bitbucket .. ?


Thanks,
Julien

On 03/21/2012 15:08, Michael Bayer wrote:

the PG dialect has only FOR UPDATE and FOR UPDATE NOWAIT at the moment, 
easy enough to add more options though it would be helpful if someone could volunteer a patch on it 
(with tests as always!):

http://www.sqlalchemy.org/trac/ticket/2445


On Mar 21, 2012, at 6:00 AM, Julien Cigar wrote:


Hello,

I'm using SQLAlchemy 0.7.6 and PostgreSQL 9.0.7 which support a FOR UPDATE and 
a FOR SHARE clause to lock selected rows.
I noticed that the .with_lockmode() method of the Query object 
(http://docs.sqlalchemy.org/en/latest/orm/query.html?highlight=with_lock#sqlalchemy.orm.query.Query.with_lockmode)
 only accepts 'update' for PostgreSQL. Could it be an oblivion .. ?

(I tried with the .with_lockmode('read'), but it also generates a FOR UPDATE on 
PostgreSQL)

Thank you,
Julien

--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

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

jcigar.vcf



--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

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

attachment: jcigar.vcf

Re: [sqlalchemy] Single table inheritance

2012-03-21 Thread Kent Bower

That will work for me, thanks!

P.S. make a note that the doc statement that it will be a future release 
should be updated.


On 3/21/2012 10:04 AM, Michael Bayer wrote:

also polymorphic_on can be any SQL expression in 0.7, like a CASE statement if 
you wanted.


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



Re: [sqlalchemy] Single table inheritance

2012-03-21 Thread Michael Bayer

On Mar 21, 2012, at 10:29 AM, Kent Bower wrote:

 That will work for me, thanks!
 
 P.S. make a note that the doc statement that it will be a future release 
 should be updated.

can you point me right to where it says that



 
 On 3/21/2012 10:04 AM, Michael Bayer wrote:
 also polymorphic_on can be any SQL expression in 0.7, like a CASE statement 
 if you wanted.
 
 -- 
 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.
 

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



Re: [sqlalchemy] Load parent on child deletion

2012-03-21 Thread Pau Tallada
Yes, I meant that :P

I'll try with the before_flush_event.

Thanks!!

2012/3/21 Michael Bayer mike...@zzzcomputing.com


 On Mar 21, 2012, at 5:18 AM, Pau Tallada wrote:

 Hi!

 I have a model with a parent-child relationship on the same entity.
 The children collection on a parent instance has events set to intercept
 adding and removing instances to/from it. This is needed to update an
 attribute from the parent instance.
 But when I delete an instance which is the child of another one, that
 parent instance is not notified of its child removal and so the event is
 not fired.

 I tried working with the cascade rules but I have not been successful.

 Is this behaviour supported?


 delete an instance, meaning,

 parent.children.append(child)
 session.delete(child)
 session.commit()

 ?


 in that case no, parent.children is not notified of a collection
 removal, since no collection alteration actually takes place.   The child
 is still there in parent.collection until the whole of parent is expired,
 which occurs after the commit().  See
 http://docs.sqlalchemy.org/en/latest/orm/session.html#deleting-from-collectionsfor
  detailed discussion on this.

 Your two options here are to stick to collection mutations, or do an
 additional before_flush() or after_flush() event that goes through child
 objects marked deleted, finds their parents, and does what is needed.




 Thanks!

 Pau.
 --
 --
 Pau Tallada Crespí
 Dep. d'Astrofísica i Cosmologia
 Port d'Informació Científica (PIC)
 Tel: +34 93 586 8233
 --



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


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




-- 
--
Pau Tallada Crespí
Dep. d'Astrofísica i Cosmologia
Port d'Informació Científica (PIC)
Tel: +34 93 586 8233
--

-- 
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: Access denied for user error

2012-03-21 Thread shinriyo
I tried sqlit. it is fine.

engine = create_engine('sqlite+pysqlite:///file.db')

but, MySQL can't connect.

2012年3月21日水曜日12時46分23秒 UTC+9 shinriyo:

 Hi there

 I use sqlalchemy and Python3.2 and pymysql on Windows7.

 I tried below

 from sqlalchemy import *

 engine = create_engine('mysql+pymysql://teaspoon:teaspoon@localhost/teaspoon')
 metadata = MetaData()
 t = Table('mytable', metadata,
 Column('mytable_id', Integer, primary_key=True)
 )
 metadata.create_all(engine)


 But, error occurd

 sqlalchemy.exc.InternalError: (InternalError) (1045, #28000Access denied for 
 user 'teaspoon'@'localhost' (using password: YES)) None None


 So, I tried via console. (DOSprompt)

 *It it connect? = It is OK!*

 c:\xampp\mysql\bin\mysql.exe -u teaspoon -p


 *Can user use database? = It is OK!*

 mysql use teaspoon
 Database changed


 *Can user create? = It is OK!* 

 mysql CREATE TABLE teaspoon(id INT(11)
 Query OK, 0 rows affected (0.07 sec)



 I have no idea.

 *Would you tell me why mysql deny?*


2012年3月21日水曜日12時46分23秒 UTC+9 shinriyo:

 Hi there

 I use sqlalchemy and Python3.2 and pymysql on Windows7.

 I tried below

 from sqlalchemy import *

 engine = create_engine('mysql+pymysql://teaspoon:teaspoon@localhost/teaspoon')
 metadata = MetaData()
 t = Table('mytable', metadata,
 Column('mytable_id', Integer, primary_key=True)
 )
 metadata.create_all(engine)


 But, error occurd

 sqlalchemy.exc.InternalError: (InternalError) (1045, #28000Access denied for 
 user 'teaspoon'@'localhost' (using password: YES)) None None


 So, I tried via console. (DOSprompt)

 *It it connect? = It is OK!*

 c:\xampp\mysql\bin\mysql.exe -u teaspoon -p


 *Can user use database? = It is OK!*

 mysql use teaspoon
 Database changed


 *Can user create? = It is OK!* 

 mysql CREATE TABLE teaspoon(id INT(11)
 Query OK, 0 rows affected (0.07 sec)



 I have no idea.

 *Would you tell me why mysql deny?*



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/F9BC0dTCw8oJ.
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.



Re: [sqlalchemy] Re: Access denied for user error

2012-03-21 Thread Michael Bayer
it seems like you connect without a host using mysql.exe, so try:

create_engine('mysql+pymysql://teaspoon:teaspoon@/teaspoon')

otherwise you need to add an entry for 'teaspoon'@'localhost' to your user 
table:

http://dev.mysql.com/doc/refman/5.0/en/connection-access.html



On Mar 21, 2012, at 10:48 PM, shinriyo wrote:

 I tried sqlit. it is fine.
 
 engine = create_engine('sqlite+pysqlite:///file.db')
 
 but, MySQL can't connect.
 
 2012年3月21日水曜日12時46分23秒 UTC+9 shinriyo:
 Hi there
 
 I use sqlalchemy and Python3.2 and pymysql on Windows7.
 
 I tried below
 
 from sqlalchemy import *
 
 engine = create_engine('mysql+pymysql://teaspoon:teaspoon@localhost/teaspoon')
 metadata = MetaData()
 t = Table('mytable', metadata,
 Column('mytable_id', Integer, primary_key=True)
 )
 metadata.create_all(engine)
 
 But, error occurd
 sqlalchemy.exc.InternalError: (InternalError) (1045, #28000Access denied for 
 user 'teaspoon'@'localhost' (using password: YES)) None None
 
 So, I tried via console. (DOSprompt)
 
 It it connect? = It is OK!
 c:\xampp\mysql\bin\mysql.exe -u teaspoon -p
 
 Can user use database? = It is OK!
 mysql use teaspoon
 Database changed
 
 Can user create? = It is OK! 
 mysql CREATE TABLE teaspoon(id INT(11)
 Query OK, 0 rows affected (0.07 sec)
 
 
 I have no idea.
 
 Would you tell me why mysql deny?
 
 
 2012年3月21日水曜日12時46分23秒 UTC+9 shinriyo:
 Hi there
 
 I use sqlalchemy and Python3.2 and pymysql on Windows7.
 
 I tried below
 
 from sqlalchemy import *
 
 engine = create_engine('mysql+pymysql://teaspoon:teaspoon@localhost/teaspoon')
 metadata = MetaData()
 t = Table('mytable', metadata,
 Column('mytable_id', Integer, primary_key=True)
 )
 metadata.create_all(engine)
 
 But, error occurd
 sqlalchemy.exc.InternalError: (InternalError) (1045, #28000Access denied for 
 user 'teaspoon'@'localhost' (using password: YES)) None None
 
 So, I tried via console. (DOSprompt)
 
 It it connect? = It is OK!
 c:\xampp\mysql\bin\mysql.exe -u teaspoon -p
 
 Can user use database? = It is OK!
 mysql use teaspoon
 Database changed
 
 Can user create? = It is OK! 
 mysql CREATE TABLE teaspoon(id INT(11)
 Query OK, 0 rows affected (0.07 sec)
 
 
 I have no idea.
 
 Would you tell me why mysql deny?
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To view this discussion on the web visit 
 https://groups.google.com/d/msg/sqlalchemy/-/F9BC0dTCw8oJ.
 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.

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



Re: [sqlalchemy] Re: Access denied for user error

2012-03-21 Thread shinriyo
*
Hi Michael Bayer 

Thank you for your help.

I tried to change create_engine args you suggested below.

create_engine('mysql+pymysql://teaspoon:teaspoon@/teaspoon') 

But, same problem was happened.

So, I create user table like the URL you tought me.
---
CREATE TABLE user
(
user VARCHAR(64),
host VARCHAR(64)
);
INSERT INTO user VALUE ('teaspoon', 'localhost');
---
But, I couldn't resolve.

*
it seems like you connect without a host using mysql.exe, so try:


 create_engine('mysql+pymysql://teaspoon:teaspoon@/teaspoon')

 otherwise you need to add an entry for 'teaspoon'@'localhost' to your user 
 table:

 http://dev.mysql.com/doc/refman/5.0/en/connection-access.html



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/JQgB8MxwfkgJ.
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.