Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-12-07 Thread Mike Bayer
the answer is here: https://stackoverflow.com/a/12943155/34549 so "Token" has to be aliased and both token and the alias need to be locked individually, demo is below. But keep in mind, since you are LOCKing the whole table, the whole "INSERT..SELECT" approach is unnecessary, you could just

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-12-06 Thread jens . troeger
> is that session using "autocommit" mode? > print("--> autocommit", dbsession.autocommit) gives a False. > it looks like the error is raised on the UNLOCK ? When I comment out the UNLOCK, the exception still raises. Here is SQLA’s verbose logging: --> autocommit False 2017-12-07

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-12-05 Thread Mike Bayer
is that session using "autocommit" mode? it looks like the error is raised on the UNLOCK ? On Wed, Dec 6, 2017 at 12:24 AM, wrote: > Once again I'm at a loss. > > I tried this: > > dbsession.execute("LOCK TABLES tokens WRITE") > dbsession.execute(cond_insert)

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-12-04 Thread jens . troeger
Thank you! I found this elderly thread: https://groups.google.com/forum/?fromgroups=#!topic/sqlalchemy/8WLhbsp2nls If injecting the SQL statements directly is still the way to go, then I’ll wrap the conditional insert with a table lock. Jens On Tuesday, December 5, 2017 at 6:00:16 AM

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-12-04 Thread Mike Bayer
On Mon, Dec 4, 2017 at 2:56 PM, wrote: > I am now thoroughly confused. > > My understanding of the above conditional insert statement was that it won’t > persist a token if there is already a token with the same user_id and > client_sig in the table. Alas, today once

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-12-04 Thread jens . troeger
I am now thoroughly confused. My understanding of the above conditional insert statement was that it won’t persist a token if there is already a token with the same user_id and client_sig in the table. Alas, today once again I see an exception “MultipleResultsFound: Multiple rows were found

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-28 Thread Mike Bayer
On Nov 28, 2017 12:13 AM, wrote: Hah 樂 Boy this is (not really) funny. Thank you for digging into this, Mike! I had to make two minor import adjustments from sqlalchemy.sql.elements import quoted_name from sqlalchemy.sql.expression import literal_column but otherwise

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-27 Thread jens . troeger
Hah 樂 Boy this is (not really) funny. Thank you for digging into this, Mike! I had to make two minor import adjustments from sqlalchemy.sql.elements import quoted_name from sqlalchemy.sql.expression import literal_column

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-27 Thread Mike Bayer
On Mon, Nov 27, 2017 at 4:02 PM, wrote: > > > No problem, here it is. To work with your initial code example... > e = create_engine("mysql+pymysql://jens@localhost/test?charset=utf8_socket=/opt/local/var/run/mysql56/mysqld.sock", echo=True)

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-27 Thread jens . troeger
No problem, here it is. To work with your initial code example... >>> e = create_engine("mysql+pymysql://jens@localhost/test?charset=utf8_socket=/opt/local/var/run/mysql56/mysqld.sock", echo=True) >>> Base.metadata.drop_all(e) 2017-11-28 06:47:21,171 INFO sqlalchemy.engine.base.Engine SHOW

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-27 Thread Mike Bayer
On Sun, Nov 26, 2017 at 9:39 PM, wrote: > Thank you, Mike! > > I’ve tried to implement what we talked about above, but had a few problems. > First, I was unable to use MySQLdb because of compilation errors; instead > I’ve used pymysql for a while now. When I tried to run

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-26 Thread jens . troeger
Thank you, Mike! I’ve tried to implement what we talked about above, but had a few problems. First, I was unable to use MySQLdb because of compilation errors; instead I’ve used pymysql for a while now.

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-26 Thread Mike Bayer
On Sun, Nov 26, 2017 at 3:49 AM, wrote: > Thanks Mike! > >> that's the first red flag here, why can't you put a unique constraint >> here? > > > Ordinarily I’d agree. In this case, there’s an additional column called > “deleted” which is NULL for active Tokens and

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-26 Thread jens . troeger
Thanks Mike! that's the first red flag here, why can't you put a unique constraint here? > Ordinarily I’d agree. In this case, there’s an additional column called “deleted” which is NULL for active Tokens and contains utcnow() for deleted Tokens. That makes for deleted and active tokens which

Re: [sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-25 Thread Mike Bayer
On Fri, Nov 24, 2017 at 6:32 PM, wrote: > Hi, > > My question is based on this answer on Stackoverflow to the question MySQL > Conditional Insert. In my current SA implementation I have the following > code: > > token = Token( > user=user, > client_sig=sigstring,

[sqlalchemy] How to implement a conditional insert (MySQL)

2017-11-24 Thread jens . troeger
Hi, My question is based on this answer on Stackoverflow to the question *MySQL Conditional Insert*. In my current SA implementation I have the following code: token = Token(