0

The question is self-explanatory.

Replace: works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted

So the query i'd like to run is

REPLACE INTO questions SET question = 'myquestions', category = 'mycategory', isNew = '0';

and my table is the following

CREATE TABLE questions (
id int(11) NOT NULL AUTO_INCREMENT,
question varchar(254) NOT NULL,
category varchar(254) NOT NULL,
isNew tinyint(1) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (category, isNew)
);

So the goal is if there is already a row with the same category AND isNew delete it before creating the new row.

prof chaos
  • 404
  • 3
  • 18

1 Answers1

1

There is the UPSERt function

See documentation

Insert or update a single row. An update will be executed if a row which matches the supplied values on either the primary key or a unique key is found. Note that the unique index must be defined in your sequelize model and not just in the table. Otherwise you may experience a unique constraint violation, because sequelize fails to identify the row that should be updated.

  Model.upsert({uniqueKey: 1234, name: 'DarthVader'}).then(function () {
  });
nbk
  • 45,398
  • 8
  • 30
  • 47