You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an entity that has updatedAt column that is updated with the current date on each update of the entity.
When I make a change to the entity - in this case I have updated FK column/property - and flush the changes to the database, I get 2 UPDATE SQL queries for the same record in the DB. One that updates updated_at column and another that updated the FK column.
Those 2 UPDATEs should be merged into one.
Reproduction
import{Entity,OneToOne,PrimaryKey,Property}from'@mikro-orm/core'import{MikroORM,PostgreSqlDriver}from'@mikro-orm/postgresql'import{mockLogger}from'../helpers'
@Entity()exportclassOther{
@PrimaryKey()id!: number
@Property()name!: string
@Property({length: 0,nullable: true,onUpdate: ()=>newDate()})updatedAt?: Date}
@Entity()exportclassTest{
@PrimaryKey()id!: number
@Property()name!: string
@Property({length: 0,nullable: true,onUpdate: ()=>newDate()})updatedAt?: Date
@OneToOne(()=>Other,{nullable: true,owner: true})other?: Other}describe('MikroORM',()=>{letorm: MikroORMbeforeAll(async()=>{orm=awaitMikroORM.init({host: 'localhost',port: 5432,user: 'mikro-orm-test',password: 'mikro-orm-test',dbName: 'mikro-orm-test',driver: PostgreSqlDriver,allowGlobalContext: true,debug: true,entities: [Test],})awaitorm.schema.refreshDatabase({wrap: true})})afterAll(async()=>{awaitorm.close(true)})it('should merge all changes of one record into one sql update statement',async()=>{consttid=awaitorm.em.insert(Test,{name: 'Foo'})awaitorm.em.flush()consttest=awaitorm.em.findOne(Test,{id: tid})constother=newOther()other.name='Foo'orm.em.persist(other)test!.other=otherconstmock=mockLogger(orm)awaitorm.em.flush()expect(mock.mock.calls).toHaveLength([['begin'],['insert into other'],['update test'],['commit']].length,)})})
The executed SQLs statements are:
begin;
insert into "other" ("name") values ('Foo') returning "id";
update "test" set "updated_at" = '2024-04-26T12:49:50.788Z' where "id" = 1;
update "test" set "other_id" = 1, "updated_at" = '2024-04-26T12:49:50.841Z' where "id" = 1;
commit;
but I would expect them to be:
begin;
insert into "other" ("name") values ('Foo') returning "id";
update "test" set "updated_at" = '2024-04-26T12:49:50.788Z', "other_id" = 1 where "id" = 1;
commit;
Describe the bug
I have an entity that has
updatedAt
column that is updated with the current date on each update of the entity.When I make a change to the entity - in this case I have updated FK column/property - and flush the changes to the database, I get 2 UPDATE SQL queries for the same record in the DB. One that updates updated_at column and another that updated the FK column.
Those 2 UPDATEs should be merged into one.
Reproduction
The executed SQLs statements are:
but I would expect them to be:
What driver are you using?
@mikro-orm/postgresql
MikroORM version
6.2.3
Node.js version
18.17.1
Operating system
MacOS 14.0
Validations
The text was updated successfully, but these errors were encountered: