@mikro-orm/knex 6.1.10-dev.1 → 6.1.10-dev.11

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.
@@ -187,6 +187,10 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
187
187
  path += '[pivot]';
188
188
  }
189
189
  const relationAlias = qb.getAliasForJoinPath(path, { matchPopulateJoins: true });
190
+ /* istanbul ignore next */
191
+ if (!relationAlias) {
192
+ return;
193
+ }
190
194
  // pivot ref joins via joined strategy need to be handled separately here, as they dont join the target entity
191
195
  if (pivotRefJoin) {
192
196
  let item;
@@ -212,10 +216,10 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
212
216
  }));
213
217
  if (!hasPK) {
214
218
  if ([core_1.ReferenceKind.MANY_TO_MANY, core_1.ReferenceKind.ONE_TO_MANY].includes(prop.kind)) {
215
- result[prop.name] ??= [];
219
+ result[prop.name] = [];
216
220
  }
217
221
  if ([core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
218
- result[prop.name] ??= null;
222
+ result[prop.name] = null;
219
223
  }
220
224
  return;
221
225
  }
@@ -709,12 +713,12 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
709
713
  const alias = qb.getNextAlias(prop.targetMeta.tableName);
710
714
  qb.leftJoin(`${targetAlias}.${hint.field}`, alias);
711
715
  // eslint-disable-next-line dot-notation
712
- Object.values(qb['_joins']).forEach(join => {
716
+ for (const join of Object.values(qb['_joins'])) {
713
717
  const [propName] = hint.field.split(':', 2);
714
718
  if (join.alias === alias && join.prop.name === propName) {
715
719
  fields.push(...qb.helper.mapJoinColumns(qb.type, join));
716
720
  }
717
- });
721
+ }
718
722
  });
719
723
  }
720
724
  qb.select(fields)
@@ -724,19 +728,30 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
724
728
  if (owners.length === 1 && (options.offset != null || options.limit != null)) {
725
729
  qb.limit(options.limit, options.offset);
726
730
  }
731
+ // console.log('pivot qb', qb, qb._fields);
727
732
  const res = owners.length ? await this.rethrow(qb.execute('all', { mergeResults: false, mapResults: false })) : [];
728
- const items = res.map((row) => super.mapResult(row, prop.targetMeta));
733
+ // console.log(res);
734
+ // const items = res.map((row: Dictionary) => super.mapResult(row, prop.targetMeta));
735
+ const tmp = {};
736
+ // const items = res.map((row: Dictionary) => this.mapResult(row, prop.targetMeta!, populate, qb, tmp));
737
+ // const items = res.map((row: Dictionary) => this.mapResult(row, pivotMeta, populate, qb, tmp));
738
+ const items = res.map((row) => {
739
+ const root = super.mapResult(row, prop.targetMeta);
740
+ this.mapJoinedProps(root, prop.targetMeta, populate, qb, root, tmp, pivotMeta.className + '.' + pivotProp1.name);
741
+ return root;
742
+ });
743
+ // console.log(prop.name, prop.targetMeta!.className, items);
729
744
  qb.clearRawFragmentsCache();
730
745
  const map = {};
731
746
  const pkProps = ownerMeta.getPrimaryProps();
732
- owners.forEach(owner => {
747
+ for (const owner of owners) {
733
748
  const key = core_1.Utils.getPrimaryKeyHash(prop.joinColumns.map((_col, idx) => {
734
749
  const pkProp = pkProps[idx];
735
750
  return pkProp.customType ? pkProp.customType.convertToJSValue(owner[idx], this.platform) : owner[idx];
736
751
  }));
737
- return map[key] = [];
738
- });
739
- items.forEach((item) => {
752
+ map[key] = [];
753
+ }
754
+ for (const item of items) {
740
755
  const key = core_1.Utils.getPrimaryKeyHash(prop.joinColumns.map((col, idx) => {
741
756
  const pkProp = pkProps[idx];
742
757
  return pkProp.customType ? pkProp.customType.convertToJSValue(item[`fk__${col}`], this.platform) : item[`fk__${col}`];
@@ -746,7 +761,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
746
761
  prop.inverseJoinColumns.forEach((col, idx) => {
747
762
  core_1.Utils.renameKey(item, `fk__${col}`, prop.targetMeta.primaryKeys[idx]);
748
763
  });
749
- });
764
+ }
750
765
  return map;
751
766
  }
752
767
  getPivotOrderBy(prop, pivotProp, pivotAlias, orderBy) {
@@ -784,13 +799,14 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
784
799
  */
785
800
  joinedProps(meta, populate, options) {
786
801
  return populate.filter(hint => {
787
- const [propName] = hint.field.split(':', 2);
802
+ const [propName, ref] = hint.field.split(':', 2);
788
803
  const prop = meta.properties[propName] || {};
789
804
  if (hint.filter && hint.strategy === core_1.LoadStrategy.JOINED) {
790
805
  return true;
791
806
  }
792
807
  if ((options?.strategy || hint.strategy || prop.strategy || this.config.get('loadStrategy')) !== core_1.LoadStrategy.JOINED) {
793
- return false;
808
+ // force joined strategy for explicit 1:1 owner populate hint as it would require a join anyway
809
+ return prop.kind === core_1.ReferenceKind.ONE_TO_ONE && !prop.owner;
794
810
  }
795
811
  return ![core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.EMBEDDED].includes(prop.kind);
796
812
  });
@@ -861,6 +877,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
861
877
  const prop = meta.properties[propName];
862
878
  // ignore ref joins of known FKs unless it's a filter hint
863
879
  if (ref && !hint.filter && (prop.kind === core_1.ReferenceKind.MANY_TO_ONE || (prop.kind === core_1.ReferenceKind.ONE_TO_ONE && !prop.owner))) {
880
+ // // console.log('wat', hint);
864
881
  return;
865
882
  }
866
883
  const meta2 = this.metadata.find(prop.type);
@@ -894,37 +911,42 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
894
911
  fields.push(...this.getFieldsForJoinedLoad(qb, meta2, childExplicitFields.length === 0 ? undefined : childExplicitFields, childExclude, hint.children, options, tableAlias, path));
895
912
  }
896
913
  else if (hint.filter) {
897
- fields.push(field);
914
+ // fields.push(field);
915
+ fields.push(...prop.referencedColumnNames.map(col => qb.helper.mapper(`${tableAlias}.${col}`, qb.type, undefined, `${tableAlias}__${col}`)));
898
916
  }
899
917
  });
918
+ // // console.log(fields, joinedProps);
900
919
  return fields;
901
920
  }
902
921
  /**
903
922
  * @internal
904
923
  */
905
924
  mapPropToFieldNames(qb, prop, tableAlias) {
906
- const knex = this.connection.getKnex();
907
- const aliased = knex.ref(tableAlias ? `${tableAlias}__${prop.fieldNames[0]}` : prop.fieldNames[0]).toString();
925
+ const aliased = this.platform.quoteIdentifier(tableAlias ? `${tableAlias}__${prop.fieldNames[0]}` : prop.fieldNames[0]);
908
926
  if (tableAlias && prop.customTypes?.some(type => type?.convertToJSValueSQL)) {
909
927
  return prop.fieldNames.map((col, idx) => {
910
928
  if (!prop.customTypes[idx]?.convertToJSValueSQL) {
911
929
  return col;
912
930
  }
913
- const prefixed = knex.ref(col).withSchema(tableAlias).toString();
914
- const aliased = knex.ref(`${tableAlias}__${col}`).toString();
931
+ const prefixed = this.platform.quoteIdentifier(`${tableAlias}.${col}`);
932
+ const aliased = this.platform.quoteIdentifier(`${tableAlias}__${col}`);
915
933
  return (0, core_1.raw)(`${prop.customTypes[idx].convertToJSValueSQL(prefixed, this.platform)} as ${aliased}`);
916
934
  });
917
935
  }
918
936
  if (tableAlias && prop.customType?.convertToJSValueSQL) {
919
- const prefixed = knex.ref(prop.fieldNames[0]).withSchema(tableAlias).toString();
937
+ const prefixed = this.platform.quoteIdentifier(`${tableAlias}.${prop.fieldNames[0]}`);
920
938
  return [(0, core_1.raw)(`${prop.customType.convertToJSValueSQL(prefixed, this.platform)} as ${aliased}`)];
921
939
  }
922
940
  if (prop.formula) {
923
- const alias = knex.ref(tableAlias ?? qb.alias).toString();
941
+ const alias = this.platform.quoteIdentifier(tableAlias ?? qb.alias);
924
942
  return [(0, core_1.raw)(`${prop.formula(alias)} as ${aliased}`)];
925
943
  }
926
944
  if (tableAlias) {
927
- return prop.fieldNames.map(fieldName => knex.ref(fieldName).withSchema(tableAlias).as(`${tableAlias}__${fieldName}`));
945
+ return prop.fieldNames.map(fieldName => {
946
+ const name = this.platform.quoteIdentifier(`${tableAlias}.${fieldName}`);
947
+ const alias = this.platform.quoteIdentifier(`${tableAlias}__${fieldName}`);
948
+ return (0, core_1.raw)(`${name} as ${alias}`);
949
+ });
928
950
  }
929
951
  return prop.fieldNames;
930
952
  }
@@ -1188,8 +1210,8 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
1188
1210
  meta.props
1189
1211
  .filter(prop => prop.formula && !lazyProps.includes(prop))
1190
1212
  .forEach(prop => {
1191
- const a = this.connection.getKnex().ref(alias).toString();
1192
- const aliased = this.connection.getKnex().ref(prop.fieldNames[0]).toString();
1213
+ const a = this.platform.quoteIdentifier(alias);
1214
+ const aliased = this.platform.quoteIdentifier(prop.fieldNames[0]);
1193
1215
  ret.push((0, core_1.raw)(`${prop.formula(a)} as ${aliased}`));
1194
1216
  });
1195
1217
  meta.props
package/README.md CHANGED
@@ -8,8 +8,7 @@ TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-or
8
8
 
9
9
  [![NPM version](https://img.shields.io/npm/v/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
10
10
  [![NPM dev version](https://img.shields.io/npm/v/@mikro-orm/core/next.svg)](https://www.npmjs.com/package/@mikro-orm/core)
11
- [![Chat on slack](https://img.shields.io/badge/chat-on%20slack-blue.svg)](https://join.slack.com/t/mikroorm/shared_invite/enQtNTM1ODYzMzM4MDk3LWM4ZDExMjU5ZDhmNjA2MmM3MWMwZmExNjhhNDdiYTMwNWM0MGY5ZTE3ZjkyZTMzOWExNDgyYmMzNDE1NDI5NjA)
12
- [![Chat on discord](https://img.shields.io/badge/chat-on%20discord-blue.svg)](https://discord.gg/ynAmaVDV)
11
+ [![Chat on discord](https://img.shields.io/badge/chat-on%20discord-blue.svg)](https://discord.gg/w8bjxFHS7X)
13
12
  [![Downloads](https://img.shields.io/npm/dm/@mikro-orm/core.svg)](https://www.npmjs.com/package/@mikro-orm/core)
14
13
  [![Coverage Status](https://img.shields.io/coveralls/mikro-orm/mikro-orm.svg)](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
15
14
  [![Maintainability](https://api.codeclimate.com/v1/badges/27999651d3adc47cfa40/maintainability)](https://codeclimate.com/github/mikro-orm/mikro-orm/maintainability)
@@ -11,7 +11,7 @@ class SqlEntityManager extends core_1.EntityManager {
11
11
  * Creates a QueryBuilder instance
12
12
  */
13
13
  createQueryBuilder(entityName, alias, type, loggerContext) {
14
- const context = this.getContext();
14
+ const context = this.getContext(false);
15
15
  return new query_1.QueryBuilder(entityName, this.getMetadata(), this.getDriver(), context.getTransactionContext(), alias, type, context, loggerContext ?? context.loggerContext);
16
16
  }
17
17
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.1.10-dev.1",
3
+ "version": "6.1.10-dev.11",
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,6 +66,6 @@
66
66
  "@mikro-orm/core": "^6.1.9"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.1.10-dev.1"
69
+ "@mikro-orm/core": "6.1.10-dev.11"
70
70
  }
71
71
  }
@@ -1072,6 +1072,7 @@ class QueryBuilder {
1072
1072
  const alias = this.getNextAlias(prop.pivotEntity ?? prop.type);
1073
1073
  const aliasedName = `${fromAlias}.${prop.name}#${alias}`;
1074
1074
  this._joins[aliasedName] = this.helper.joinOneToReference(prop, this.mainAlias.aliasName, alias, enums_1.JoinType.leftJoin);
1075
+ this._joins[aliasedName].path = `${(Object.values(this._joins).find(j => j.alias === fromAlias)?.path ?? meta.className)}.${prop.name}`;
1075
1076
  this._populateMap[aliasedName] = this._joins[aliasedName].alias;
1076
1077
  }
1077
1078
  });
@@ -1131,6 +1132,10 @@ class QueryBuilder {
1131
1132
  if (join.cond[k]) {
1132
1133
  join.cond = { [op ?? '$and']: [join.cond, { [k]: cond[k] }] };
1133
1134
  }
1135
+ else if (op === '$or') {
1136
+ join.cond.$or ??= [];
1137
+ join.cond.$or.push({ [k]: cond[k] });
1138
+ }
1134
1139
  else {
1135
1140
  join.cond = { ...join.cond, [k]: cond[k] };
1136
1141
  }
@@ -193,8 +193,10 @@ class QueryBuilderHelper {
193
193
  conditions.push(`${left} = ${this.knex.ref(right)}`);
194
194
  return;
195
195
  }
196
- const left = `${join.ownerAlias}.${primaryKey}`;
197
- conditions.push(`${this.knex.ref(left)} = ${this.knex.ref(right)}`);
196
+ const left = join.prop.object && join.prop.fieldNameRaw
197
+ ? join.prop.fieldNameRaw.replaceAll(core_1.ALIAS_REPLACEMENT, join.ownerAlias)
198
+ : this.knex.ref(`${join.ownerAlias}.${primaryKey}`);
199
+ conditions.push(`${left} = ${this.knex.ref(right)}`);
198
200
  });
199
201
  }
200
202
  if (join.prop.targetMeta?.discriminatorValue && !join.path?.endsWith('[pivot]')) {
@@ -295,7 +297,7 @@ class QueryBuilderHelper {
295
297
  if (join.prop && [core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.ONE_TO_ONE].includes(join.prop.kind)) {
296
298
  return join.prop.fieldNames.map((fieldName, idx) => {
297
299
  const columns = join.prop.owner ? join.joinColumns : join.inverseJoinColumns;
298
- return this.mapper(`${join.alias}.${columns[idx]}`, type, undefined, fieldName);
300
+ return this.mapper(`${join.alias}.${columns[idx]}`, type, undefined, `${join.alias}__${columns[idx]}`);
299
301
  });
300
302
  }
301
303
  return [
@@ -16,9 +16,23 @@ export declare class DatabaseSchema {
16
16
  getTables(): DatabaseTable[];
17
17
  getTable(name: string): DatabaseTable | undefined;
18
18
  hasTable(name: string): boolean;
19
- setNativeEnums(nativeEnums: Dictionary<unknown[]>): void;
20
- getNativeEnums(): Dictionary<unknown[]>;
19
+ setNativeEnums(nativeEnums: Dictionary<{
20
+ name: string;
21
+ schema?: string;
22
+ items: string[];
23
+ }>): void;
24
+ getNativeEnums(): Dictionary<{
25
+ name: string;
26
+ schema?: string;
27
+ items: string[];
28
+ }>;
29
+ getNativeEnum(name: string): {
30
+ name: string;
31
+ schema?: string;
32
+ items: string[];
33
+ };
21
34
  hasNamespace(namespace: string): boolean;
35
+ hasNativeEnum(name: string): boolean;
22
36
  getNamespaces(): string[];
23
37
  static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[]): Promise<DatabaseSchema>;
24
38
  static fromMetadata(metadata: EntityMetadata[], platform: AbstractSqlPlatform, config: Configuration, schemaName?: string): DatabaseSchema;
@@ -43,9 +43,15 @@ class DatabaseSchema {
43
43
  getNativeEnums() {
44
44
  return this.nativeEnums;
45
45
  }
46
+ getNativeEnum(name) {
47
+ return this.nativeEnums[name];
48
+ }
46
49
  hasNamespace(namespace) {
47
50
  return this.namespaces.has(namespace);
48
51
  }
52
+ hasNativeEnum(name) {
53
+ return name in this.nativeEnums;
54
+ }
49
55
  getNamespaces() {
50
56
  return [...this.namespaces];
51
57
  }
@@ -63,9 +69,20 @@ class DatabaseSchema {
63
69
  const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema'));
64
70
  const nativeEnums = {};
65
71
  for (const meta of metadata) {
66
- meta.props
67
- .filter(prop => prop.nativeEnumName)
68
- .forEach(prop => nativeEnums[prop.nativeEnumName] = prop.items?.map(val => '' + val) ?? []);
72
+ for (const prop of meta.props) {
73
+ if (prop.nativeEnumName) {
74
+ let key = prop.nativeEnumName;
75
+ const s = meta.schema ?? schema.name;
76
+ if (s && s !== platform.getDefaultSchemaName()) {
77
+ key = s + '.' + key;
78
+ }
79
+ nativeEnums[key] = {
80
+ name: prop.nativeEnumName,
81
+ schema: meta.schema ?? schema.name,
82
+ items: prop.items?.map(val => '' + val) ?? [],
83
+ };
84
+ }
85
+ }
69
86
  }
70
87
  schema.setNativeEnums(nativeEnums);
71
88
  for (const meta of metadata) {
@@ -13,7 +13,11 @@ export declare class DatabaseTable {
13
13
  private indexes;
14
14
  private checks;
15
15
  private foreignKeys;
16
- nativeEnums: Dictionary<unknown[]>;
16
+ nativeEnums: Dictionary<{
17
+ name: string;
18
+ schema?: string;
19
+ items: string[];
20
+ }>;
17
21
  comment?: string;
18
22
  constructor(platform: AbstractSqlPlatform, name: string, schema?: string | undefined);
19
23
  getColumns(): Column[];
@@ -23,7 +23,17 @@ class SchemaComparator {
23
23
  * stored in toSchema.
24
24
  */
25
25
  compare(fromSchema, toSchema, inverseDiff) {
26
- const diff = { newTables: {}, removedTables: {}, changedTables: {}, orphanedForeignKeys: [], newNamespaces: new Set(), removedNamespaces: new Set(), fromSchema };
26
+ const diff = {
27
+ newTables: {},
28
+ removedTables: {},
29
+ changedTables: {},
30
+ orphanedForeignKeys: [],
31
+ newNativeEnums: [],
32
+ removedNativeEnums: [],
33
+ newNamespaces: new Set(),
34
+ removedNamespaces: new Set(),
35
+ fromSchema,
36
+ };
27
37
  const foreignKeysToTable = {};
28
38
  for (const namespace of toSchema.getNamespaces()) {
29
39
  if (fromSchema.hasNamespace(namespace) || namespace === this.platform.getDefaultSchemaName()) {
@@ -37,6 +47,18 @@ class SchemaComparator {
37
47
  }
38
48
  diff.removedNamespaces.add(namespace);
39
49
  }
50
+ for (const nativeEnum of Object.keys(toSchema.getNativeEnums())) {
51
+ if (fromSchema.hasNativeEnum(nativeEnum)) {
52
+ continue;
53
+ }
54
+ diff.newNativeEnums.push(toSchema.getNativeEnum(nativeEnum));
55
+ }
56
+ for (const nativeEnum of Object.keys(fromSchema.getNativeEnums())) {
57
+ if (toSchema.hasNativeEnum(nativeEnum)) {
58
+ continue;
59
+ }
60
+ diff.removedNativeEnums.push(fromSchema.getNativeEnum(nativeEnum));
61
+ }
40
62
  for (const table of toSchema.getTables()) {
41
63
  const tableName = table.getShortestName();
42
64
  if (!fromSchema.hasTable(tableName)) {
@@ -68,7 +68,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
68
68
  continue;
69
69
  }
70
70
  created.push(enumName);
71
- const sql = this.helper.getCreateNativeEnumSQL(enumName, enumOptions, options.schema ?? this.config.get('schema'));
71
+ const sql = this.helper.getCreateNativeEnumSQL(enumName, enumOptions.items, options.schema ?? this.config.get('schema'));
72
72
  ret += await this.dump(this.knex.schema.raw(sql), '\n');
73
73
  }
74
74
  }
@@ -180,7 +180,6 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
180
180
  const wildcardSchemaTables = Object.values(this.metadata.getAll()).filter(meta => meta.schema === '*').map(meta => meta.tableName);
181
181
  fromSchema.prune(options.schema, wildcardSchemaTables);
182
182
  toSchema.prune(options.schema, wildcardSchemaTables);
183
- toSchema.setNativeEnums(fromSchema.getNativeEnums());
184
183
  return { fromSchema, toSchema };
185
184
  }
186
185
  async diffToSQL(schemaDiff, options) {
@@ -191,6 +190,12 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
191
190
  ret += await this.dump(this.knex.schema.createSchemaIfNotExists(newNamespace));
192
191
  }
193
192
  }
193
+ if (this.platform.supportsNativeEnums()) {
194
+ for (const newNativeEnum of schemaDiff.newNativeEnums) {
195
+ const sql = this.helper.getCreateNativeEnumSQL(newNativeEnum.name, newNativeEnum.items, newNativeEnum.schema ?? options.schema ?? this.config.get('schema'));
196
+ ret += await this.dump(this.knex.schema.raw(sql), '\n');
197
+ }
198
+ }
194
199
  if (!options.safe) {
195
200
  for (const orphanedForeignKey of schemaDiff.orphanedForeignKeys) {
196
201
  const [schemaName, tableName] = this.splitTableName(orphanedForeignKey.localTableName);
@@ -227,6 +232,12 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
227
232
  ret += await this.dump(builder);
228
233
  }
229
234
  }
235
+ if (!options.safe && this.platform.supportsNativeEnums()) {
236
+ for (const removedNativeEnum of schemaDiff.removedNativeEnums) {
237
+ const sql = this.helper.getDropNativeEnumSQL(removedNativeEnum.name, removedNativeEnum.schema);
238
+ ret += await this.dump(this.knex.schema.raw(sql), '\n');
239
+ }
240
+ }
230
241
  if (options.dropTables && !options.safe) {
231
242
  for (const removedNamespace of schemaDiff.removedNamespaces) {
232
243
  ret += await this.dump(this.knex.schema.dropSchema(removedNamespace));
@@ -296,22 +307,16 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
296
307
  const ret = [];
297
308
  const [schemaName, tableName] = this.splitTableName(diff.name);
298
309
  if (this.platform.supportsNativeEnums()) {
299
- const createdNativeEnums = [];
300
310
  const changedNativeEnums = [];
301
- for (const { column, changedProperties, fromColumn } of Object.values(diff.changedColumns)) {
311
+ for (const { column, changedProperties } of Object.values(diff.changedColumns)) {
302
312
  if (!column.nativeEnumName) {
303
313
  continue;
304
314
  }
305
- if (changedProperties.has('enumItems') && column.nativeEnumName in diff.fromTable.nativeEnums) {
306
- changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.getColumn(column.name).enumItems]);
307
- }
308
- else if (changedProperties.has('type') && !(column.nativeEnumName in diff.fromTable.nativeEnums)) {
309
- createdNativeEnums.push([column.nativeEnumName, column.enumItems]);
315
+ const key = schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' + column.nativeEnumName : column.nativeEnumName;
316
+ if (changedProperties.has('enumItems') && key in diff.fromTable.nativeEnums) {
317
+ changedNativeEnums.push([column.nativeEnumName, column.enumItems, diff.fromTable.nativeEnums[key].items]);
310
318
  }
311
319
  }
312
- core_1.Utils.removeDuplicates(createdNativeEnums).forEach(([enumName, items]) => {
313
- ret.push(this.knex.schema.raw(this.helper.getCreateNativeEnumSQL(enumName, items, schemaName)));
314
- });
315
320
  core_1.Utils.removeDuplicates(changedNativeEnums).forEach(([enumName, itemsNew, itemsOld]) => {
316
321
  // postgres allows only adding new items, the values are case insensitive
317
322
  itemsOld = itemsOld.map(v => v.toLowerCase());
package/typings.d.ts CHANGED
@@ -107,10 +107,19 @@ export interface TableDifference {
107
107
  }
108
108
  export interface SchemaDifference {
109
109
  newNamespaces: Set<string>;
110
+ newNativeEnums: {
111
+ name: string;
112
+ schema?: string;
113
+ items: string[];
114
+ }[];
110
115
  newTables: Dictionary<DatabaseTable>;
111
116
  changedTables: Dictionary<TableDifference>;
112
117
  removedTables: Dictionary<DatabaseTable>;
113
118
  removedNamespaces: Set<string>;
119
+ removedNativeEnums: {
120
+ name: string;
121
+ schema?: string;
122
+ }[];
114
123
  orphanedForeignKeys: ForeignKey[];
115
124
  fromSchema: DatabaseSchema;
116
125
  }