Skip to content
Advertisement

Facing problem updating a column of a table, while the id and alias column remains same – sqlalchemy

Hi my table named ‘total’ looks like this,

JavaScript

If I want to insert a new row with a new alias lets say alias = ‘lion’ and status = ‘gold’ it should create a new id and the table should look like this

JavaScript

But if I keep the alias same as ‘lion’ and status = ‘silver’ it should just update the row with id = 2 and it should look like this

JavaScript

How to achieve it in sqlalchemy using insert command?

the id is primary key and alias value is stored in a variable a and status value is stored in a variable b, which looks like

JavaScript

Advertisement

Answer

Looks like your alias is unique identifier for the object/row. In this case an approach of find-and-update OR create should work:

JavaScript

See documentation:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement