Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL Upsert on_duplicate_key_update #4483

Closed
bcochofel opened this issue Feb 4, 2019 · 6 comments
Closed

MySQL Upsert on_duplicate_key_update #4483

bcochofel opened this issue Feb 4, 2019 · 6 comments
Labels
question issue where a "fix" on the SQLAlchemy side is unlikely, hence more of a usage question

Comments

@bcochofel
Copy link

Hi,

I'm trying to upsert a table that has a column named "id" (primary key) and a column named "name" (unique key).

The var "value" is a dict, and the following code gives me:

TypeError: on_duplicate_key_update() takes exactly 1 argument (2 given)

try:                                                          
    stmt = insert(dbmeta.tables['Assets']).values(values)     
    ustmt = stmt.on_duplicate_key_update(values)              
    dbconn.execute(ustmt)                                     
    trans.commit()                                            
except exc.SQLAlchemyError as e:                              
    print("[ ERROR ] - Rolling back transation (%s)" % e, file=sys.stderr)                
    trans.rollback()                                          
    return False                                              
return True                                                   

What am I doing wrong?

Thanks,
Bruno

@zzzeek
Copy link
Member

zzzeek commented Feb 4, 2019

@zzzeek zzzeek added the question issue where a "fix" on the SQLAlchemy side is unlikely, hence more of a usage question label Feb 4, 2019
@bcochofel
Copy link
Author

here are the 1.2 docs:

https://docs.sqlalchemy.org/en/rel_1_2/dialects/mysql.html?highlight=on_duplicate_key_update#sqlalchemy.dialects.mysql.dml.Insert.on_duplicate_key_update

the single dictionary form is new in 1.3

Hi, thanks for the quick answer.

Should I describe every column that I want to update? The think is, my dict varies according to the parameters that are passed, so not all the columns go to the dict.
How can I handle this dynamically?

@zzzeek
Copy link
Member

zzzeek commented Feb 4, 2019

are you looking for Python star syntax?

ustmt = stmt.on_duplicate_key_update(**values) 

@bcochofel
Copy link
Author

are you looking for Python star syntax?

ustmt = stmt.on_duplicate_key_update(**values) 

Hi zzzeek, not quite sure what you mean?

@zzzeek
Copy link
Member

zzzeek commented Feb 5, 2019

I'm trying to answer your question "how can I handle this dynamically?" the usage you have attempted at the top of this question needs only the modification of using **kwargs syntax. can you please just try this and let me know it solves your problem, thanks.

@bcochofel
Copy link
Author

Hi, ok, now I understood, thanks.

Closing this topic.

Bruno

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question issue where a "fix" on the SQLAlchemy side is unlikely, hence more of a usage question
Projects
None yet
Development

No branches or pull requests

2 participants