mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-07 14:39:25 +08:00
fix: update table value without primary keys (#6124)
* fix: update table value without primary keys * fix: test * fix: test
This commit is contained in:
parent
b4eda8d04a
commit
f66a48abd0
@ -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: {
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user