fix: update table value without primary keys (#6124)

* fix: update table value without primary keys

* fix: test

* fix: test
This commit is contained in:
ChengLei Shao 2025-01-23 10:45:54 +08:00 committed by GitHub
parent b4eda8d04a
commit f66a48abd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 0 deletions

View File

@ -87,6 +87,40 @@ describe('update', () => {
await db.sync();
});
it('should update collection that without primary key', async () => {
const collection = db.collection({
name: 'without_pk',
autoGenId: false,
timestamps: false,
fields: [{ type: 'string', name: 'nameWithUnderscore' }],
});
await collection.sync();
await collection.repository.create({
values: {
nameWithUnderscore: 'item1',
},
});
await collection.repository.update({
values: {
nameWithUnderscore: 'item2',
},
filter: {
nameWithUnderscore: 'item1',
},
});
const item = await collection.repository.findOne({
filter: {
nameWithUnderscore: 'item2',
},
});
expect(item).toBeDefined();
});
it('should throw error when update data conflicted', async () => {
const t1 = await Tag.repository.create({
values: {

View File

@ -22,6 +22,7 @@ import {
FindOptions as SequelizeFindOptions,
UpdateOptions as SequelizeUpdateOptions,
Transactionable,
Utils,
WhereOperators,
} from 'sequelize';
@ -420,6 +421,10 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
rows = eagerLoadingTree.root.instances;
} else {
if (opts.where && model.primaryKeyAttributes.length === 0) {
opts.where = Utils.mapWhereFieldNames(opts.where, model);
}
rows = await model.findAll({
...opts,
transaction,