Laravel upsert only if two columns conflict
I have the following table
I have the following table
would like to know if it’s possible to insert or upsert in Laravel by keeping the Autoincrement id blank.
here is my table migration
Is there an easy way to INSERT
a row when it does not exist, or to UPDATE
if it exists, using one MySQL query?
I want to add a row to a database table, but if a row exists with the same unique key I want to update the row.
Stupid but simple example:
Assume I have a table ‘Item’ where I keeps totals of the items that receive.
MySQL has something like this:
I have the following table of counters:
From MySQL 4.1.0 onwards, it is possible to add ON DUPLICATE KEY UPDATE
statement to specify behavior when values inserted (with INSERT
or SET
or VALUES
) are already in destination table w.r.t. PRIMARY KEY
or some UNIQUE
field. If value for PRIMARY KEY
or some UNIQUE
field are already in table, INSERT
is replaced by an UPDATE
.
I’ve got a MySql table with an auto-increment primary key, and it seems that all of the various upsert methods (INSERT IGNORE and ON DUPLICATE KEY UPDATE) suffer from the, uh, feature that the auto-increment field increments, even if a row is updated and not inserted. This means that gaps are introduced into the table, which I find undesirable.
I am using CakePHP 3 and MySQL.