[Python] Insert if doesn't exist - in python+sqlalchemy

Discussão em 'Python' iniciado por Stack, Fevereiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    The problem: find an rdbms-independent way to get a value with the specified key from a database using sqlalchemy! If the key doesn't exist, generate a new value and insert it to the table. In a multiuser+multiprocess environment...

    I have two ideas: 1st get it, if it returns None, then generate a new and insert, if it is unsuccessful (because of duplicate key) then get it again.

    2nd: lock the table, get it, if returns None, then generate a new and insert

    The 2nd seems to be better, but I can't find an rdbms-independent solution for locking a table. (session.execute('lock ...') is inappropriate, because it uses sql directly)

    The 1st looks... khm... unprofessional. How can I solve this?

    Continue reading...

Compartilhe esta Página