Open artsiommiksiuk opened 6 days ago
6.2.0
Please always try latest version before you report anything.
Also, I'd appreciate a complete reproduction instead of those snippets.
Not reproducible on my end, here is a passing reproduction based on what you provided, let's reopen with a failing one.
import {
Entity,
ManyToOne,
MikroORM,
PrimaryKey,
PrimaryKeyProp,
Property,
ref,
Ref,
} from '@mikro-orm/postgresql';
@Entity()
class Organization {
@PrimaryKey({ type: 'uuid' })
id!: string;
@Property()
name!: string;
}
@Entity()
class Sample {
@ManyToOne(() => Organization, { primary: true })
organization!: Ref<Organization>;
@PrimaryKey({ type: 'uuid' })
id!: string;
[PrimaryKeyProp]?: ['organization', 'id'];
}
@Entity({ tableName: 'sample-stick-well' })
class SampleStickWell {
@ManyToOne(() => Sample, { primary: true })
sample!: Ref<Sample>;
[PrimaryKeyProp]?: 'sample';
@Property()
createdAt = new Date();
@Property()
updatedAt = new Date();
@Property()
stickWellId!: string;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
dbName: '6252',
entities: [SampleStickWell],
debug: true,
});
await orm.schema.refreshDatabase();
});
afterAll(async () => {
await orm.close(true);
});
test('6252', async () => {
const oid = 'ac21172f-98b0-436f-aeda-923d5e61748f';
const sid = '4953caed-cb20-4cb4-939e-958b1874a090';
await orm.em.insert(Organization, { id: oid, name: 'org' });
await orm.em.insert(Sample, { id: sid, organization: oid });
await orm.em.upsertMany(SampleStickWell, [{
sample: ref(Sample, [oid, sid]),
stickWellId: 'value',
createdAt: new Date(),
updatedAt: new Date(),
}]);
});
@B4nan, sorry for the initial poor description. I've managed to make a reproducible based on your attempt with latest (6.4.0) version.
Turned out, that created as class instances entities are not working, while simple objects are fine.
import {
Entity,
ManyToOne,
MikroORM,
PrimaryKey,
PrimaryKeyProp,
Property,
ref,
type Ref,
} from '@mikro-orm/postgresql';
import test, { after, before } from 'node:test';
@Entity()
export class Organization {
@PrimaryKey({ type: 'uuid' })
id!: string;
@Property()
name!: string;
}
@Entity()
export class Sample {
@ManyToOne(() => Organization, { primary: true })
organization!: Ref<Organization>;
@PrimaryKey({ type: 'uuid' })
id!: string;
[PrimaryKeyProp]?: ['organization', 'id'];
}
@Entity()
export class SampleStickWell {
@ManyToOne(() => Sample, { primary: true })
sample!: Ref<Sample>;
[PrimaryKeyProp]?: ['sample'];
@Property()
createdAt = new Date();
@Property()
updatedAt = new Date();
@Property()
stickWellId!: string;
}
let orm: MikroORM;
const oid = 'ac21172f-98b0-436f-aeda-923d5e61748f';
const sid = '4953caed-cb20-4cb4-939e-958b1874a090';
before(async () => {
orm = await MikroORM.init({
dbName: '6252',
entities: [SampleStickWell],
debug: true,
allowGlobalContext: true,
});
await orm.schema.refreshDatabase();
await orm.em.insert(Organization, { id: oid, name: 'org' });
await orm.em.insert(Sample, { id: sid, organization: oid });
});
after(async () => {
await orm.close(true);
});
// not working
test('6252 class constructor', async () => {
const ssw = new SampleStickWell();
ssw.sample = ref(Sample, [oid, sid]);
ssw.stickWellId = "value";
ssw.createdAt = new Date();
ssw.updatedAt = new Date();
await orm.em.upsertMany(SampleStickWell, [ssw]);
});
// working
test('6252 object literal', async () => {
await orm.em.upsertMany(SampleStickWell, [{
sample: ref(Sample, [oid, sid]),
stickWellId: 'value',
createdAt: new Date(),
updatedAt: new Date(),
}]);
});
Thanks, will take another look later today.
Describe the bug
UpsertMany with composite keys coming from foreign entity generates wrong SQL.
This is cleanedup generated sql from reproduction issue:
Looks like generated on conflict section must be:
Instead of
Workaround I've found, which proving my guess, is manually setting the
onConflictFields
to correct values works.Reproduction
The setup is quite lengthy. Sorry for that.
This should be the call itself.
Error:
What driver are you using?
@mikro-orm/postgresql
MikroORM version
6.2.0
Node.js version
20.11.1
Operating system
MacOS
Validations