@mikro-orm/knex 6.6.18-dev.0 → 6.6.18-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -842,7 +842,11 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
842
842
  map[key] = [];
843
843
  }
844
844
  for (const item of res) {
845
- const key = core_1.Utils.getPrimaryKeyHash(core_1.Utils.asArray(item[pivotProp2.name]));
845
+ const fk = item[pivotProp2.name];
846
+ // the owner PKs are always flat, while the pivot FK follows the owner PK structure,
847
+ // so a PK built from a relation to another composite PK entity needs flattening too
848
+ const pks = fk != null ? core_1.Utils.getOrderedPrimaryKeys(fk, ownerMeta) : core_1.Utils.asArray(fk);
849
+ const key = core_1.Utils.getPrimaryKeyHash(pks);
846
850
  map[key].push(item[pivotProp1.name]);
847
851
  }
848
852
  return map;
@@ -917,8 +921,24 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
917
921
  const [propName, ref] = hint.field.split(':', 2);
918
922
  return { propName, ref, children: hint.children };
919
923
  });
924
+ // with `fixedOrder` the pivot PK is the order column, which is not guaranteed to be unique when the
925
+ // pivot table is managed externally, so we disambiguate the rows by their FKs on top of the PK
926
+ const pivotRelations = meta.pivotTable && !meta.compositePK
927
+ ? meta.relations.filter(p => p.kind === core_1.ReferenceKind.MANY_TO_ONE && p.persist !== false)
928
+ : [];
920
929
  for (const item of rawResults) {
921
- const pk = core_1.Utils.getCompositeKeyHash(item, meta);
930
+ // flat hash, so nested composite PK values keep their own separators and cannot collide
931
+ let pk = core_1.Utils.getCompositeKeyHash(item, meta, false, undefined, true);
932
+ if (pivotRelations.length > 0) {
933
+ pk = core_1.Utils.getPrimaryKeyHash([
934
+ pk,
935
+ ...pivotRelations.flatMap(p => {
936
+ const value = item[p.name];
937
+ // composite FKs are mapped to an array of values, which `extractPK` does not accept
938
+ return (Array.isArray(value) ? core_1.Utils.flatten(value, true) : core_1.Utils.extractPK(value, p.targetMeta));
939
+ }),
940
+ ]);
941
+ }
922
942
  if (map[pk]) {
923
943
  for (const { propName } of hints) {
924
944
  if (!item[propName]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.6.18-dev.0",
3
+ "version": "6.6.18-dev.2",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -66,7 +66,7 @@
66
66
  "@mikro-orm/core": "^6.6.17"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.6.18-dev.0",
69
+ "@mikro-orm/core": "6.6.18-dev.2",
70
70
  "better-sqlite3": "*",
71
71
  "libsql": "*",
72
72
  "mariadb": "*"