Skip to content

Commit

Permalink
fix(core): ensure correct FK as PK identity map key
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Apr 2, 2023
1 parent 75653f0 commit 475e2a3
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/entity/EntityFactory.ts
Expand Up @@ -256,7 +256,7 @@ export class EntityFactory {
private findEntity<T extends object>(data: EntityData<T>, meta: EntityMetadata<T>, options: FactoryOptions): T | undefined {
const schema = this.driver.getSchemaName(meta, options);

if (!meta.compositePK && !meta.getPrimaryProps()[0]?.customType) {
if (meta.simplePK) {
return this.unitOfWork.getById<T>(meta.name!, data[meta.primaryKeys[0] as string] as Primary<T>, schema);
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/metadata/EntitySchema.ts
Expand Up @@ -304,6 +304,7 @@ export class EntitySchema<T = any, U = never> {
if (pks.length > 0) {
this._meta.primaryKeys = pks.map(prop => prop.name);
this._meta.compositePK = pks.length > 1;
this._meta.simplePK = !this._meta.compositePK && pks[0].reference === ReferenceType.SCALAR && !pks[0].customType;
}

if (pks.length === 1 && pks[0].type === 'number') {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/metadata/MetadataDiscovery.ts
Expand Up @@ -487,6 +487,7 @@ export class MetadataDiscovery {
this.initRelation(prop);
}

meta.simplePK = pks.length === 1 && pks[0].reference === ReferenceType.SCALAR && !pks[0].customType;
meta.serializedPrimaryKey = this.platform.getSerializedPrimaryKeyField(meta.primaryKeys[0]);
const serializedPKProp = meta.properties[meta.serializedPrimaryKey];

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/typings.ts
Expand Up @@ -519,6 +519,7 @@ export interface EntityMetadata<T = any> {
collection: string;
path: string;
primaryKeys: (keyof T & string)[];
simplePK: boolean; // whether the PK can be compared via `===`, e.g. simple scalar without a custom mapped type
compositePK: boolean;
versionProperty: keyof T & string;
concurrencyCheckKeys: Set<keyof T & string>;
Expand Down

0 comments on commit 475e2a3

Please sign in to comment.