On Sun, Nov 26, 2017 at 9:39 PM,  <jens.troe...@gmail.com> 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 the test code from
> your previous answer I received a SQL syntax error near WHERE. It looks like
> the FROM is missing.

OK I don't recall what conditions MySQL needs "FROM dual" can you share with me:

1. the full version of MySQL

2. the output of "SHOW VARIABLES LIKE '%SQL_MODE%'

3. stack trace + error message


>
> As for “fails” I was mistaken. The conditional insert should always succeed,
> but may not insert a row into the table. While this maintains data
> consistency, it’s not quite what I need. It’ll do for now though.
>
> My current working solution is the following hack which I need to revisit
> again later:
>
> # Create a proxy object to return to the caller.
> token = Token(
>     id=uuid4(),
>     user=user_id,
>     client_sig=sigstring,
>     )
> # If I don't expunge then the token is automatically added/inserted?
> # I thought I'd always have to dbsession.add().
> dbsession.expunge(token)
>
> # Now issue the SQL statement much like the on from my initial question.
> sql = (
>     "INSERT INTO tokens(id, user_id, client_sig) "
>     "SELECT '" + token.id + "', '" + token.user_id + "', '" +
> token.client_sig + "' "
>     "FROM dual "
>     "WHERE NOT EXISTS "
>     "(SELECT * FROM tokens WHERE user_id='" + token.user_id + "' AND
> client_sig='" + token.client_sig + "')"
>     )
> dbsession.execute(sql)
>
> # Return the proxy object to the Pyramid view function.
> return token
>
> I realize that this is a poor solution because the returned proxy object may
> in rare cases not represent what's committed to the db. However, that's
> easier to handle than committing a duplicate. I would also prefer SQLA
> functions over raw SQL code.
>
> Jens
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to