sequelize 是否具有相当于 mysqls 的替换功能?
Does sequelize have an equivalent of mysqls replace function?
问题不言自明。
替换:工作方式与 INSERT 完全相同,不同之处在于如果 table 中的旧行与 PRIMARY KEY 或 UNIQUE 索引的新行具有相同的值,则旧行将在新行之前被删除插入行
所以我想要运行的查询是
REPLACE INTO questions SET question = 'myquestions', category = 'mycategory', isNew = '0';
我的table如下
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)
);
所以目标是如果已经有一个具有相同类别的行并且在创建新行之前将其删除。
有UPSERt功能
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 () {
});
问题不言自明。
替换:工作方式与 INSERT 完全相同,不同之处在于如果 table 中的旧行与 PRIMARY KEY 或 UNIQUE 索引的新行具有相同的值,则旧行将在新行之前被删除插入行
所以我想要运行的查询是
REPLACE INTO questions SET question = 'myquestions', category = 'mycategory', isNew = '0';
我的table如下
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)
);
所以目标是如果已经有一个具有相同类别的行并且在创建新行之前将其删除。
有UPSERt功能
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 () {
});