@mikro-orm/sql 7.2.5-dev.7 → 7.2.5-dev.9

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.
@@ -127,6 +127,12 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
127
127
  * @internal
128
128
  */
129
129
  protected addTPTPolymorphicJoinsForRelation<T extends object>(qb: AnyQueryBuilder<T>, meta: EntityMetadata<T>, baseAlias: string, fields: InternalField<T>[]): void;
130
+ /**
131
+ * Registers the ancestor table aliases of each polymorphically joined TPT descendant,
132
+ * so formulas on descendants resolve inherited columns to the table that owns them.
133
+ * @internal
134
+ */
135
+ registerTPTAncestorAliases(qb: AnyQueryBuilder<any>, meta: EntityMetadata, baseAlias: string, childAliases: Dictionary<string>): void;
130
136
  /**
131
137
  * Find the alias for a TPT child table in the query builder.
132
138
  * @internal
@@ -1989,6 +1989,10 @@ export class AbstractSqlDriver extends DatabaseDriver {
1989
1989
  qb.createAlias(childMeta.class, childAlias);
1990
1990
  childAliases[childMeta.className] = childAlias;
1991
1991
  qb.addPropertyJoin(childMeta.tptInverseProp, baseAlias, childAlias, JoinType.leftJoin, `[tpt]${meta.className}`);
1992
+ }
1993
+ this.registerTPTAncestorAliases(qb, meta, baseAlias, childAliases);
1994
+ for (const childMeta of descendants) {
1995
+ const childAlias = childAliases[childMeta.className];
1992
1996
  // Add fields from this child (only ownProps, skip PKs)
1993
1997
  const schema = childMeta.schema === '*' ? '*' : this.getSchemaName(childMeta);
1994
1998
  childMeta
@@ -2000,6 +2004,21 @@ export class AbstractSqlDriver extends DatabaseDriver {
2000
2004
  fields.push(this.buildTPTDiscriminatorExpression(meta, descendants, childAliases, baseAlias));
2001
2005
  }
2002
2006
  }
2007
+ /**
2008
+ * Registers the ancestor table aliases of each polymorphically joined TPT descendant,
2009
+ * so formulas on descendants resolve inherited columns to the table that owns them.
2010
+ * @internal
2011
+ */
2012
+ registerTPTAncestorAliases(qb, meta, baseAlias, childAliases) {
2013
+ const tptAlias = qb.state.tptAlias;
2014
+ for (const childMeta of meta.allTPTDescendants) {
2015
+ for (let parent = childMeta.tptParent; parent; parent = parent.tptParent) {
2016
+ tptAlias[`${childAliases[childMeta.className]}:${parent.className}`] =
2017
+ childAliases[parent.className] ??
2018
+ (parent === meta ? baseAlias : tptAlias[`${baseAlias}:${parent.className}`]);
2019
+ }
2020
+ }
2021
+ }
2003
2022
  /**
2004
2023
  * Find the alias for a TPT child table in the query builder.
2005
2024
  * @internal
@@ -2090,7 +2109,7 @@ export class AbstractSqlDriver extends DatabaseDriver {
2090
2109
  if (prop.formula) {
2091
2110
  const quotedAlias = this.platform.quoteIdentifier(tableAlias).toString();
2092
2111
  const table = this.createFormulaTable(quotedAlias, meta, schema);
2093
- const columns = meta.createColumnMappingObject(tableAlias);
2112
+ const columns = meta.createColumnMappingObject(p => qb.helper.getTPTAliasForProperty(p.name, tableAlias), tableAlias);
2094
2113
  return [raw(`${this.evaluateFormula(prop.formula, columns, table)} as ${aliased}`)];
2095
2114
  }
2096
2115
  if (prop.customTypes?.some(type => !!type?.convertToJSValueSQL)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.2.5-dev.7",
3
+ "version": "7.2.5-dev.9",
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
  "keywords": [
6
6
  "data-mapper",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.2.4"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.5-dev.7"
56
+ "@mikro-orm/core": "7.2.5-dev.9"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -1943,7 +1943,8 @@ export class QueryBuilder {
1943
1943
  if (parentAlias) {
1944
1944
  const schema = parentMeta.schema === '*' ? '*' : this.driver.getSchemaName(parentMeta);
1945
1945
  parentMeta
1946
- .ownProps.filter(prop => this.platform.shouldHaveColumn(prop, []))
1946
+ // inherited formulas are selected on the main alias in `finalize()`
1947
+ .ownProps.filter(prop => !prop.formula && this.platform.shouldHaveColumn(prop, []))
1947
1948
  .forEach(prop => this.#state.fields.push(...this.driver.mapPropToFieldNames(this, prop, parentAlias, parentMeta, schema)));
1948
1949
  }
1949
1950
  parentMeta = parentMeta.tptParent;
@@ -1962,12 +1963,16 @@ export class QueryBuilder {
1962
1963
  if (!this.#state.fields?.includes('*') && !this.#state.fields?.includes(`${this.mainAlias.aliasName}.*`)) {
1963
1964
  return;
1964
1965
  }
1965
- // LEFT JOIN each descendant table and add their fields
1966
+ // LEFT JOIN each descendant table
1966
1967
  for (const childMeta of descendants) {
1967
1968
  const childAlias = this.getNextAlias(childMeta.className);
1968
1969
  this.createAlias(childMeta.class, childAlias);
1969
1970
  this.#state.tptAlias[childMeta.className] = childAlias;
1970
1971
  this.addPropertyJoin(childMeta.tptInverseProp, this.mainAlias.aliasName, childAlias, JoinType.leftJoin, `[tpt]${meta.className}`);
1972
+ }
1973
+ this.driver.registerTPTAncestorAliases(this, meta, this.mainAlias.aliasName, this.#state.tptAlias);
1974
+ for (const childMeta of descendants) {
1975
+ const childAlias = this.#state.tptAlias[childMeta.className];
1971
1976
  // Add child fields
1972
1977
  const schema = childMeta.schema === '*' ? '*' : this.driver.getSchemaName(childMeta);
1973
1978
  childMeta
@@ -1976,7 +1981,7 @@ export class QueryBuilder {
1976
1981
  }
1977
1982
  // Add computed discriminator (CASE WHEN to determine concrete type)
1978
1983
  // descendants is pre-sorted by depth (deepest first) during discovery
1979
- if (meta.tptDiscriminatorColumn) {
1984
+ if (meta.root.tptDiscriminatorColumn) {
1980
1985
  this.#state.fields.push(this.driver.buildTPTDiscriminatorExpression(meta, descendants, this.#state.tptAlias, this.mainAlias.aliasName));
1981
1986
  }
1982
1987
  }