Skip to content

Commit

Permalink
fix: update with empty conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyDaddy committed Dec 17, 2021
1 parent f3f6207 commit b4af36a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/drivers/abstract/spellbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ function formatUpdate(spell) {
}
}

for (const condition of whereConditions) collectLiteral(spell, condition, values);
// see https://dev.mysql.com/doc/refman/8.0/en/optimizer-hints.html
const hintStr = this.formatOptimizerHints(spell);
// see https://dev.mysql.com/doc/refman/8.0/en/index-hints.html
Expand All @@ -472,7 +471,10 @@ function formatUpdate(spell) {
}

chunks.push(`SET ${assigns.join(', ')}`);
chunks.push(`WHERE ${formatConditions(spell, whereConditions)}`);
if (whereConditions.length > 0) {
for (const condition of whereConditions) collectLiteral(spell, condition, values);
chunks.push(`WHERE ${formatConditions(spell, whereConditions)}`);
}
return {
sql: chunks.join(' '),
values,
Expand Down
11 changes: 11 additions & 0 deletions test/integration/suite/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,17 @@ describe('=> Basic', () => {
assert.deepEqual(post.extra, { versions: [ 2, 3, 4, 5 ] });
});

it('Bone.update({}, values) should work', async function() {
const posts = await Promise.all([
Post.create({ title: 'New Post' }),
Post.create({ title: 'New Post 2'})
]);
const affectedRows = await Post.update({}, { title: 'Untitled' });
expect(posts.length).to.equal(affectedRows);
const newPosts = await Post.all;
assert(newPosts.every(p => p.title === 'Untitled'));
});

it('bone.save() should UPDATE when primaryKey is defined and saved before', async function() {
const post = await Post.create({ id: 1, title: 'New Post', createdAt: new Date(2010, 9, 11) });
const updatedAtWas = post.updatedAt;
Expand Down

0 comments on commit b4af36a

Please sign in to comment.