Hi,

My question is based on this answer 
<https://stackoverflow.com/questions/913841/mysql-conditional-insert#913929> 
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,                                                  
                      
    )                                                                      
                     
session.add(token)                                                          
              
# session.flush()                                                          
                     

Now I would like to prevent duplicate client_sig entries to prevent the 
same user having the same client_sig, but I can’t set that column to 
unique. It looks like the following SQL statement achieves what I want:

INSERT INTO tokens(id, user_id, client_sig)
SELECT '51…bb', 'fd…b3', 'some string'
FROM dual
WHERE NOT EXISTS (
  SELECT * 
  FROM tokens
  WHERE user_id='fd…b3'
  AND client_sig='some string'
  );

I found some documentation on insert … from select 
<http://docs.sqlalchemy.org/en/latest/core/dml.html#sqlalchemy.sql.expression.Insert.from_select>
 
but can not quite put the two together (I don't think they're the same). 
How would I go about implementing the above SQL statement using SA, or will 
I have to issue raw SQL in this case?

Thank you!
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.

Reply via email to