@payloadcms/db-postgres 3.0.0-beta.11 → 3.0.0-beta.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable no-param-reassign */ import { relations } from 'drizzle-orm';
2
- import { index, integer, numeric, serial, timestamp, unique, varchar } from 'drizzle-orm/pg-core';
2
+ import { foreignKey, index, integer, numeric, serial, timestamp, unique, varchar } from 'drizzle-orm/pg-core';
3
+ import toSnakeCase from 'to-snake-case';
3
4
  import { getTableName } from './getTableName.js';
4
5
  import { parentIDColumnMap } from './parentIDColumnMap.js';
5
6
  import { setColumnID } from './setColumnID.js';
@@ -61,25 +62,33 @@ export const buildTable = ({ adapter, baseColumns = {}, baseExtraConfig = {}, bu
61
62
  config[key] = func(cols);
62
63
  return config;
63
64
  }, {});
64
- return Object.entries(indexes).reduce((acc, [colName, func])=>{
65
+ const result = Object.entries(indexes).reduce((acc, [colName, func])=>{
65
66
  acc[colName] = func(cols);
66
67
  return acc;
67
68
  }, extraConfig);
69
+ return result;
68
70
  });
69
71
  adapter.tables[tableName] = table;
70
72
  if (hasLocalizedField) {
71
73
  const localeTableName = `${tableName}${adapter.localesSuffix}`;
72
74
  localesColumns.id = serial('id').primaryKey();
73
75
  localesColumns._locale = adapter.enums.enum__locales('_locale').notNull();
74
- localesColumns._parentID = parentIDColumnMap[idColType]('_parent_id').references(()=>table.id, {
75
- onDelete: 'cascade'
76
- }).notNull();
76
+ localesColumns._parentID = parentIDColumnMap[idColType]('_parent_id').notNull();
77
77
  localesTable = adapter.pgSchema.table(localeTableName, localesColumns, (cols)=>{
78
78
  return Object.entries(localesIndexes).reduce((acc, [colName, func])=>{
79
79
  acc[colName] = func(cols);
80
80
  return acc;
81
81
  }, {
82
- _localeParent: unique(`${localeTableName}_locale_parent_id_unique`).on(cols._locale, cols._parentID)
82
+ _localeParent: unique(`${localeTableName}_locale_parent_id_unique`).on(cols._locale, cols._parentID),
83
+ _parentIdFk: foreignKey({
84
+ name: `${localeTableName}_parent_id_fk`,
85
+ columns: [
86
+ cols._parentID
87
+ ],
88
+ foreignColumns: [
89
+ table.id
90
+ ]
91
+ }).onDelete('cascade')
83
92
  });
84
93
  });
85
94
  adapter.tables[localeTableName] = localesTable;
@@ -100,9 +109,7 @@ export const buildTable = ({ adapter, baseColumns = {}, baseExtraConfig = {}, bu
100
109
  const columns = {
101
110
  id: serial('id').primaryKey(),
102
111
  order: integer('order').notNull(),
103
- parent: parentIDColumnMap[idColType]('parent_id').references(()=>table.id, {
104
- onDelete: 'cascade'
105
- }).notNull(),
112
+ parent: parentIDColumnMap[idColType]('parent_id').notNull(),
106
113
  path: varchar('path').notNull(),
107
114
  text: varchar('text')
108
115
  };
@@ -110,16 +117,25 @@ export const buildTable = ({ adapter, baseColumns = {}, baseExtraConfig = {}, bu
110
117
  columns.locale = adapter.enums.enum__locales('locale');
111
118
  }
112
119
  textsTable = adapter.pgSchema.table(textsTableName, columns, (cols)=>{
113
- const indexes = {
114
- orderParentIdx: index(`${textsTableName}_order_parent_idx`).on(cols.order, cols.parent)
120
+ const config = {
121
+ orderParentIdx: index(`${textsTableName}_order_parent_idx`).on(cols.order, cols.parent),
122
+ parentFk: foreignKey({
123
+ name: `${textsTableName}_parent_fk`,
124
+ columns: [
125
+ cols.parent
126
+ ],
127
+ foreignColumns: [
128
+ table.id
129
+ ]
130
+ }).onDelete('cascade')
115
131
  };
116
132
  if (hasManyTextField === 'index') {
117
- indexes.text_idx = index(`${textsTableName}_text_idx`).on(cols.text);
133
+ config.text_idx = index(`${textsTableName}_text_idx`).on(cols.text);
118
134
  }
119
135
  if (hasLocalizedManyTextField) {
120
- indexes.localeParent = index(`${textsTableName}_locale_parent`).on(cols.locale, cols.parent);
136
+ config.localeParent = index(`${textsTableName}_locale_parent`).on(cols.locale, cols.parent);
121
137
  }
122
- return indexes;
138
+ return config;
123
139
  });
124
140
  adapter.tables[textsTableName] = textsTable;
125
141
  const textsTableRelations = relations(textsTable, ({ one })=>({
@@ -140,25 +156,32 @@ export const buildTable = ({ adapter, baseColumns = {}, baseExtraConfig = {}, bu
140
156
  id: serial('id').primaryKey(),
141
157
  number: numeric('number'),
142
158
  order: integer('order').notNull(),
143
- parent: parentIDColumnMap[idColType]('parent_id').references(()=>table.id, {
144
- onDelete: 'cascade'
145
- }).notNull(),
159
+ parent: parentIDColumnMap[idColType]('parent_id').notNull(),
146
160
  path: varchar('path').notNull()
147
161
  };
148
162
  if (hasLocalizedManyNumberField) {
149
163
  columns.locale = adapter.enums.enum__locales('locale');
150
164
  }
151
165
  numbersTable = adapter.pgSchema.table(numbersTableName, columns, (cols)=>{
152
- const indexes = {
153
- orderParentIdx: index(`${numbersTableName}_order_parent_idx`).on(cols.order, cols.parent)
166
+ const config = {
167
+ orderParentIdx: index(`${numbersTableName}_order_parent_idx`).on(cols.order, cols.parent),
168
+ parentFk: foreignKey({
169
+ name: `${numbersTableName}_parent_fk`,
170
+ columns: [
171
+ cols.parent
172
+ ],
173
+ foreignColumns: [
174
+ table.id
175
+ ]
176
+ }).onDelete('cascade')
154
177
  };
155
178
  if (hasManyNumberField === 'index') {
156
- indexes.numberIdx = index(`${numbersTableName}_number_idx`).on(cols.number);
179
+ config.numberIdx = index(`${numbersTableName}_number_idx`).on(cols.number);
157
180
  }
158
181
  if (hasLocalizedManyNumberField) {
159
- indexes.localeParent = index(`${numbersTableName}_locale_parent`).on(cols.locale, cols.parent);
182
+ config.localeParent = index(`${numbersTableName}_locale_parent`).on(cols.locale, cols.parent);
160
183
  }
161
- return indexes;
184
+ return config;
162
185
  });
163
186
  adapter.tables[numbersTableName] = numbersTable;
164
187
  const numbersTableRelations = relations(numbersTable, ({ one })=>({
@@ -178,14 +201,14 @@ export const buildTable = ({ adapter, baseColumns = {}, baseExtraConfig = {}, bu
178
201
  const relationshipColumns = {
179
202
  id: serial('id').primaryKey(),
180
203
  order: integer('order'),
181
- parent: parentIDColumnMap[idColType]('parent_id').references(()=>table.id, {
182
- onDelete: 'cascade'
183
- }).notNull(),
204
+ parent: parentIDColumnMap[idColType]('parent_id').notNull(),
184
205
  path: varchar('path').notNull()
185
206
  };
186
207
  if (hasLocalizedRelationshipField) {
187
208
  relationshipColumns.locale = adapter.enums.enum__locales('locale');
188
209
  }
210
+ const relationExtraConfig = {};
211
+ const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`;
189
212
  relationships.forEach((relationTo)=>{
190
213
  const relationshipConfig = adapter.payload.collections[relationTo].config;
191
214
  const formattedRelationTo = getTableName({
@@ -197,17 +220,35 @@ export const buildTable = ({ adapter, baseColumns = {}, baseExtraConfig = {}, bu
197
220
  const relatedCollectionCustomIDType = adapter.payload.collections[relationshipConfig.slug]?.customIDType;
198
221
  if (relatedCollectionCustomIDType === 'number') colType = 'numeric';
199
222
  if (relatedCollectionCustomIDType === 'text') colType = 'varchar';
200
- relationshipColumns[`${relationTo}ID`] = parentIDColumnMap[colType](`${formattedRelationTo}_id`).references(()=>adapter.tables[formattedRelationTo].id, {
201
- onDelete: 'cascade'
202
- });
223
+ relationshipColumns[`${relationTo}ID`] = parentIDColumnMap[colType](`${formattedRelationTo}_id`);
224
+ relationExtraConfig[`${relationTo}IdFk`] = (cols)=>foreignKey({
225
+ name: `${relationshipsTableName}_${toSnakeCase(relationTo)}_fk`,
226
+ columns: [
227
+ cols[`${relationTo}ID`]
228
+ ],
229
+ foreignColumns: [
230
+ adapter.tables[formattedRelationTo].id
231
+ ]
232
+ }).onDelete('cascade');
203
233
  });
204
- const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`;
205
234
  relationshipsTable = adapter.pgSchema.table(relationshipsTableName, relationshipColumns, (cols)=>{
206
- const result = {
235
+ const result = Object.entries(relationExtraConfig).reduce((config, [key, func])=>{
236
+ config[key] = func(cols);
237
+ return config;
238
+ }, {
207
239
  order: index(`${relationshipsTableName}_order_idx`).on(cols.order),
240
+ parentFk: foreignKey({
241
+ name: `${relationshipsTableName}_parent_fk`,
242
+ columns: [
243
+ cols.parent
244
+ ],
245
+ foreignColumns: [
246
+ table.id
247
+ ]
248
+ }).onDelete('cascade'),
208
249
  parentIdx: index(`${relationshipsTableName}_parent_idx`).on(cols.parent),
209
250
  pathIdx: index(`${relationshipsTableName}_path_idx`).on(cols.path)
210
- };
251
+ });
211
252
  if (hasLocalizedRelationshipField) {
212
253
  result.localeIdx = index(`${relationshipsTableName}_locale_idx`).on(cols.locale);
213
254
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/schema/build.ts"],"sourcesContent":["/* eslint-disable no-param-reassign */\nimport type { Relation } from 'drizzle-orm'\nimport type {\n IndexBuilder,\n PgColumnBuilder,\n PgTableWithColumns,\n UniqueConstraintBuilder,\n} from 'drizzle-orm/pg-core'\nimport type { Field } from 'payload/types'\n\nimport { relations } from 'drizzle-orm'\nimport { index, integer, numeric, serial, timestamp, unique, varchar } from 'drizzle-orm/pg-core'\nimport { fieldAffectsData } from 'payload/types'\n\nimport type { GenericColumns, GenericTable, IDType, PostgresAdapter } from '../types.js'\n\nimport { getTableName } from './getTableName.js'\nimport { parentIDColumnMap } from './parentIDColumnMap.js'\nimport { setColumnID } from './setColumnID.js'\nimport { traverseFields } from './traverseFields.js'\n\ntype Args = {\n adapter: PostgresAdapter\n baseColumns?: Record<string, PgColumnBuilder>\n baseExtraConfig?: Record<string, (cols: GenericColumns) => IndexBuilder | UniqueConstraintBuilder>\n buildNumbers?: boolean\n buildRelationships?: boolean\n buildTexts?: boolean\n disableNotNull: boolean\n disableUnique: boolean\n fields: Field[]\n rootRelationsToBuild?: Map<string, string>\n rootRelationships?: Set<string>\n rootTableIDColType?: string\n rootTableName?: string\n tableName: string\n timestamps?: boolean\n versions: boolean\n}\n\ntype Result = {\n hasManyNumberField: 'index' | boolean\n hasManyTextField: 'index' | boolean\n relationsToBuild: Map<string, string>\n}\n\nexport const buildTable = ({\n adapter,\n baseColumns = {},\n baseExtraConfig = {},\n buildNumbers,\n buildRelationships,\n buildTexts,\n disableNotNull,\n disableUnique = false,\n fields,\n rootRelationsToBuild,\n rootRelationships,\n rootTableIDColType,\n rootTableName: incomingRootTableName,\n tableName,\n timestamps,\n versions,\n}: Args): Result => {\n const rootTableName = incomingRootTableName || tableName\n const columns: Record<string, PgColumnBuilder> = baseColumns\n const indexes: Record<string, (cols: GenericColumns) => IndexBuilder> = {}\n\n const localesColumns: Record<string, PgColumnBuilder> = {}\n const localesIndexes: Record<string, (cols: GenericColumns) => IndexBuilder> = {}\n let localesTable: GenericTable | PgTableWithColumns<any>\n let textsTable: GenericTable | PgTableWithColumns<any>\n let numbersTable: GenericTable | PgTableWithColumns<any>\n\n // Relationships to the base collection\n const relationships: Set<string> = rootRelationships || new Set()\n\n let relationshipsTable: GenericTable | PgTableWithColumns<any>\n\n // Drizzle relations\n const relationsToBuild: Map<string, string> = new Map()\n\n const idColType: IDType = setColumnID({ adapter, columns, fields })\n\n const {\n hasLocalizedField,\n hasLocalizedManyNumberField,\n hasLocalizedManyTextField,\n hasLocalizedRelationshipField,\n hasManyNumberField,\n hasManyTextField,\n } = traverseFields({\n adapter,\n buildNumbers,\n buildRelationships,\n buildTexts,\n columns,\n disableNotNull,\n disableUnique,\n fields,\n indexes,\n localesColumns,\n localesIndexes,\n newTableName: tableName,\n parentTableName: tableName,\n relationsToBuild,\n relationships,\n rootRelationsToBuild: rootRelationsToBuild || relationsToBuild,\n rootTableIDColType: rootTableIDColType || idColType,\n rootTableName,\n versions,\n })\n\n if (timestamps) {\n columns.createdAt = timestamp('created_at', {\n mode: 'string',\n precision: 3,\n withTimezone: true,\n })\n .defaultNow()\n .notNull()\n columns.updatedAt = timestamp('updated_at', {\n mode: 'string',\n precision: 3,\n withTimezone: true,\n })\n .defaultNow()\n .notNull()\n }\n\n const table = adapter.pgSchema.table(tableName, columns, (cols) => {\n const extraConfig = Object.entries(baseExtraConfig).reduce((config, [key, func]) => {\n config[key] = func(cols)\n return config\n }, {})\n\n return Object.entries(indexes).reduce((acc, [colName, func]) => {\n acc[colName] = func(cols)\n return acc\n }, extraConfig)\n })\n\n adapter.tables[tableName] = table\n\n if (hasLocalizedField) {\n const localeTableName = `${tableName}${adapter.localesSuffix}`\n localesColumns.id = serial('id').primaryKey()\n localesColumns._locale = adapter.enums.enum__locales('_locale').notNull()\n localesColumns._parentID = parentIDColumnMap[idColType]('_parent_id')\n .references(() => table.id, { onDelete: 'cascade' })\n .notNull()\n\n localesTable = adapter.pgSchema.table(localeTableName, localesColumns, (cols) => {\n return Object.entries(localesIndexes).reduce(\n (acc, [colName, func]) => {\n acc[colName] = func(cols)\n return acc\n },\n {\n _localeParent: unique(`${localeTableName}_locale_parent_id_unique`).on(\n cols._locale,\n cols._parentID,\n ),\n },\n )\n })\n\n adapter.tables[localeTableName] = localesTable\n\n const localesTableRelations = relations(localesTable, ({ one }) => ({\n _parentID: one(table, {\n fields: [localesTable._parentID],\n references: [table.id],\n }),\n }))\n\n adapter.relations[`relations_${localeTableName}`] = localesTableRelations\n }\n\n if (hasManyTextField && buildTexts) {\n const textsTableName = `${rootTableName}_texts`\n const columns: Record<string, PgColumnBuilder> = {\n id: serial('id').primaryKey(),\n order: integer('order').notNull(),\n parent: parentIDColumnMap[idColType]('parent_id')\n .references(() => table.id, { onDelete: 'cascade' })\n .notNull(),\n path: varchar('path').notNull(),\n text: varchar('text'),\n }\n\n if (hasLocalizedManyTextField) {\n columns.locale = adapter.enums.enum__locales('locale')\n }\n\n textsTable = adapter.pgSchema.table(textsTableName, columns, (cols) => {\n const indexes: Record<string, IndexBuilder> = {\n orderParentIdx: index(`${textsTableName}_order_parent_idx`).on(cols.order, cols.parent),\n }\n\n if (hasManyTextField === 'index') {\n indexes.text_idx = index(`${textsTableName}_text_idx`).on(cols.text)\n }\n\n if (hasLocalizedManyTextField) {\n indexes.localeParent = index(`${textsTableName}_locale_parent`).on(cols.locale, cols.parent)\n }\n\n return indexes\n })\n\n adapter.tables[textsTableName] = textsTable\n\n const textsTableRelations = relations(textsTable, ({ one }) => ({\n parent: one(table, {\n fields: [textsTable.parent],\n references: [table.id],\n }),\n }))\n\n adapter.relations[`relations_${textsTableName}`] = textsTableRelations\n }\n\n if (hasManyNumberField && buildNumbers) {\n const numbersTableName = `${rootTableName}_numbers`\n const columns: Record<string, PgColumnBuilder> = {\n id: serial('id').primaryKey(),\n number: numeric('number'),\n order: integer('order').notNull(),\n parent: parentIDColumnMap[idColType]('parent_id')\n .references(() => table.id, { onDelete: 'cascade' })\n .notNull(),\n path: varchar('path').notNull(),\n }\n\n if (hasLocalizedManyNumberField) {\n columns.locale = adapter.enums.enum__locales('locale')\n }\n\n numbersTable = adapter.pgSchema.table(numbersTableName, columns, (cols) => {\n const indexes: Record<string, IndexBuilder> = {\n orderParentIdx: index(`${numbersTableName}_order_parent_idx`).on(cols.order, cols.parent),\n }\n\n if (hasManyNumberField === 'index') {\n indexes.numberIdx = index(`${numbersTableName}_number_idx`).on(cols.number)\n }\n\n if (hasLocalizedManyNumberField) {\n indexes.localeParent = index(`${numbersTableName}_locale_parent`).on(\n cols.locale,\n cols.parent,\n )\n }\n\n return indexes\n })\n\n adapter.tables[numbersTableName] = numbersTable\n\n const numbersTableRelations = relations(numbersTable, ({ one }) => ({\n parent: one(table, {\n fields: [numbersTable.parent],\n references: [table.id],\n }),\n }))\n\n adapter.relations[`relations_${numbersTableName}`] = numbersTableRelations\n }\n\n if (buildRelationships) {\n if (relationships.size) {\n const relationshipColumns: Record<string, PgColumnBuilder> = {\n id: serial('id').primaryKey(),\n order: integer('order'),\n parent: parentIDColumnMap[idColType]('parent_id')\n .references(() => table.id, { onDelete: 'cascade' })\n .notNull(),\n path: varchar('path').notNull(),\n }\n\n if (hasLocalizedRelationshipField) {\n relationshipColumns.locale = adapter.enums.enum__locales('locale')\n }\n\n relationships.forEach((relationTo) => {\n const relationshipConfig = adapter.payload.collections[relationTo].config\n const formattedRelationTo = getTableName({\n adapter,\n config: relationshipConfig,\n throwValidationError: true,\n })\n let colType = adapter.idType === 'uuid' ? 'uuid' : 'integer'\n\n const relatedCollectionCustomIDType =\n adapter.payload.collections[relationshipConfig.slug]?.customIDType\n\n if (relatedCollectionCustomIDType === 'number') colType = 'numeric'\n if (relatedCollectionCustomIDType === 'text') colType = 'varchar'\n\n relationshipColumns[`${relationTo}ID`] = parentIDColumnMap[colType](\n `${formattedRelationTo}_id`,\n ).references(() => adapter.tables[formattedRelationTo].id, { onDelete: 'cascade' })\n })\n\n const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`\n\n relationshipsTable = adapter.pgSchema.table(\n relationshipsTableName,\n relationshipColumns,\n (cols) => {\n const result: Record<string, unknown> = {\n order: index(`${relationshipsTableName}_order_idx`).on(cols.order),\n parentIdx: index(`${relationshipsTableName}_parent_idx`).on(cols.parent),\n pathIdx: index(`${relationshipsTableName}_path_idx`).on(cols.path),\n }\n\n if (hasLocalizedRelationshipField) {\n result.localeIdx = index(`${relationshipsTableName}_locale_idx`).on(cols.locale)\n }\n\n return result\n },\n )\n\n adapter.tables[relationshipsTableName] = relationshipsTable\n\n const relationshipsTableRelations = relations(relationshipsTable, ({ one }) => {\n const result: Record<string, Relation<string>> = {\n parent: one(table, {\n fields: [relationshipsTable.parent],\n references: [table.id],\n relationName: '_rels',\n }),\n }\n\n relationships.forEach((relationTo) => {\n const relatedTableName = getTableName({\n adapter,\n config: adapter.payload.collections[relationTo].config,\n throwValidationError: true,\n })\n const idColumnName = `${relationTo}ID`\n result[idColumnName] = one(adapter.tables[relatedTableName], {\n fields: [relationshipsTable[idColumnName]],\n references: [adapter.tables[relatedTableName].id],\n })\n })\n\n return result\n })\n\n adapter.relations[`relations_${relationshipsTableName}`] = relationshipsTableRelations\n }\n }\n\n const tableRelations = relations(table, ({ many }) => {\n const result: Record<string, Relation<string>> = {}\n\n relationsToBuild.forEach((val, key) => {\n result[key] = many(adapter.tables[val])\n })\n\n if (hasLocalizedField) {\n result._locales = many(localesTable)\n }\n\n if (hasManyTextField) {\n result._texts = many(textsTable)\n }\n if (hasManyNumberField) {\n result._numbers = many(numbersTable)\n }\n\n if (relationships.size && relationshipsTable) {\n result._rels = many(relationshipsTable, {\n relationName: '_rels',\n })\n }\n\n return result\n })\n\n adapter.relations[`relations_${tableName}`] = tableRelations\n\n return { hasManyNumberField, hasManyTextField, relationsToBuild }\n}\n"],"names":["relations","index","integer","numeric","serial","timestamp","unique","varchar","getTableName","parentIDColumnMap","setColumnID","traverseFields","buildTable","adapter","baseColumns","baseExtraConfig","buildNumbers","buildRelationships","buildTexts","disableNotNull","disableUnique","fields","rootRelationsToBuild","rootRelationships","rootTableIDColType","rootTableName","incomingRootTableName","tableName","timestamps","versions","columns","indexes","localesColumns","localesIndexes","localesTable","textsTable","numbersTable","relationships","Set","relationshipsTable","relationsToBuild","Map","idColType","hasLocalizedField","hasLocalizedManyNumberField","hasLocalizedManyTextField","hasLocalizedRelationshipField","hasManyNumberField","hasManyTextField","newTableName","parentTableName","createdAt","mode","precision","withTimezone","defaultNow","notNull","updatedAt","table","pgSchema","cols","extraConfig","Object","entries","reduce","config","key","func","acc","colName","tables","localeTableName","localesSuffix","id","primaryKey","_locale","enums","enum__locales","_parentID","references","onDelete","_localeParent","on","localesTableRelations","one","textsTableName","order","parent","path","text","locale","orderParentIdx","text_idx","localeParent","textsTableRelations","numbersTableName","number","numberIdx","numbersTableRelations","size","relationshipColumns","forEach","relationTo","relationshipConfig","payload","collections","formattedRelationTo","throwValidationError","colType","idType","relatedCollectionCustomIDType","slug","customIDType","relationshipsTableName","relationshipsSuffix","result","parentIdx","pathIdx","localeIdx","relationshipsTableRelations","relationName","relatedTableName","idColumnName","tableRelations","many","val","_locales","_texts","_numbers","_rels"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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M,CAACuD,IAAI;QACxC;QAEA,IAAIlF,mBAAmB;YACrBwE,OAAOW,QAAQ,GAAGF,KAAK1F;QACzB;QAEA,IAAIc,kBAAkB;YACpBmE,OAAOY,MAAM,GAAGH,KAAKzF;QACvB;QACA,IAAIY,oBAAoB;YACtBoE,OAAOa,QAAQ,GAAGJ,KAAKxF;QACzB;QAEA,IAAIC,cAAc8D,IAAI,IAAI5D,oBAAoB;YAC5C4E,OAAOc,KAAK,GAAGL,KAAKrF,oBAAoB;gBACtCiF,cAAc;YAChB;QACF;QAEA,OAAOL;IACT;IAEAtG,QAAQb,SAAS,CAAC,CAAC,UAAU,EAAE2B,UAAU,CAAC,CAAC,GAAGgG;IAE9C,OAAO;QAAE5E;QAAoBC;QAAkBR;IAAiB;AAClE,EAAC"}
1
+ {"version":3,"sources":["../../src/schema/build.ts"],"sourcesContent":["/* eslint-disable no-param-reassign */\nimport type { Relation } from 'drizzle-orm'\nimport type {\n ForeignKeyBuilder,\n IndexBuilder,\n PgColumnBuilder,\n PgTableWithColumns,\n UniqueConstraintBuilder,\n} from 'drizzle-orm/pg-core'\nimport type { Field } from 'payload/types'\n\nimport { relations } from 'drizzle-orm'\nimport {\n foreignKey,\n index,\n integer,\n numeric,\n serial,\n timestamp,\n unique,\n varchar,\n} from 'drizzle-orm/pg-core'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { GenericColumns, GenericTable, IDType, PostgresAdapter } from '../types.js'\n\nimport { getTableName } from './getTableName.js'\nimport { parentIDColumnMap } from './parentIDColumnMap.js'\nimport { setColumnID } from './setColumnID.js'\nimport { traverseFields } from './traverseFields.js'\n\nexport type BaseExtraConfig = Record<\n string,\n (cols: GenericColumns) => ForeignKeyBuilder | IndexBuilder | UniqueConstraintBuilder\n>\n\ntype Args = {\n adapter: PostgresAdapter\n baseColumns?: Record<string, PgColumnBuilder>\n baseExtraConfig?: BaseExtraConfig\n buildNumbers?: boolean\n buildRelationships?: boolean\n buildTexts?: boolean\n disableNotNull: boolean\n disableUnique: boolean\n fields: Field[]\n rootRelationsToBuild?: Map<string, string>\n rootRelationships?: Set<string>\n rootTableIDColType?: string\n rootTableName?: string\n tableName: string\n timestamps?: boolean\n versions: boolean\n}\n\ntype Result = {\n hasManyNumberField: 'index' | boolean\n hasManyTextField: 'index' | boolean\n relationsToBuild: Map<string, string>\n}\n\nexport const buildTable = ({\n adapter,\n baseColumns = {},\n baseExtraConfig = {},\n buildNumbers,\n buildRelationships,\n buildTexts,\n disableNotNull,\n disableUnique = false,\n fields,\n rootRelationsToBuild,\n rootRelationships,\n rootTableIDColType,\n rootTableName: incomingRootTableName,\n tableName,\n timestamps,\n versions,\n}: Args): Result => {\n const rootTableName = incomingRootTableName || tableName\n const columns: Record<string, PgColumnBuilder> = baseColumns\n const indexes: Record<string, (cols: GenericColumns) => IndexBuilder> = {}\n\n const localesColumns: Record<string, PgColumnBuilder> = {}\n const localesIndexes: Record<string, (cols: GenericColumns) => IndexBuilder> = {}\n let localesTable: GenericTable | PgTableWithColumns<any>\n let textsTable: GenericTable | PgTableWithColumns<any>\n let numbersTable: GenericTable | PgTableWithColumns<any>\n\n // Relationships to the base collection\n const relationships: Set<string> = rootRelationships || new Set()\n\n let relationshipsTable: GenericTable | PgTableWithColumns<any>\n\n // Drizzle relations\n const relationsToBuild: Map<string, string> = new Map()\n\n const idColType: IDType = setColumnID({ adapter, columns, fields })\n\n const {\n hasLocalizedField,\n hasLocalizedManyNumberField,\n hasLocalizedManyTextField,\n hasLocalizedRelationshipField,\n hasManyNumberField,\n hasManyTextField,\n } = traverseFields({\n adapter,\n buildNumbers,\n buildRelationships,\n buildTexts,\n columns,\n disableNotNull,\n disableUnique,\n fields,\n indexes,\n localesColumns,\n localesIndexes,\n newTableName: tableName,\n parentTableName: tableName,\n relationsToBuild,\n relationships,\n rootRelationsToBuild: rootRelationsToBuild || relationsToBuild,\n rootTableIDColType: rootTableIDColType || idColType,\n rootTableName,\n versions,\n })\n\n if (timestamps) {\n columns.createdAt = timestamp('created_at', {\n mode: 'string',\n precision: 3,\n withTimezone: true,\n })\n .defaultNow()\n .notNull()\n columns.updatedAt = timestamp('updated_at', {\n mode: 'string',\n precision: 3,\n withTimezone: true,\n })\n .defaultNow()\n .notNull()\n }\n\n const table = adapter.pgSchema.table(tableName, columns, (cols) => {\n const extraConfig = Object.entries(baseExtraConfig).reduce((config, [key, func]) => {\n config[key] = func(cols)\n return config\n }, {})\n\n const result = Object.entries(indexes).reduce((acc, [colName, func]) => {\n acc[colName] = func(cols)\n return acc\n }, extraConfig)\n\n return result\n })\n\n adapter.tables[tableName] = table\n\n if (hasLocalizedField) {\n const localeTableName = `${tableName}${adapter.localesSuffix}`\n localesColumns.id = serial('id').primaryKey()\n localesColumns._locale = adapter.enums.enum__locales('_locale').notNull()\n localesColumns._parentID = parentIDColumnMap[idColType]('_parent_id').notNull()\n\n localesTable = adapter.pgSchema.table(localeTableName, localesColumns, (cols) => {\n return Object.entries(localesIndexes).reduce(\n (acc, [colName, func]) => {\n acc[colName] = func(cols)\n return acc\n },\n {\n _localeParent: unique(`${localeTableName}_locale_parent_id_unique`).on(\n cols._locale,\n cols._parentID,\n ),\n _parentIdFk: foreignKey({\n name: `${localeTableName}_parent_id_fk`,\n columns: [cols._parentID],\n foreignColumns: [table.id],\n }).onDelete('cascade'),\n },\n )\n })\n\n adapter.tables[localeTableName] = localesTable\n\n const localesTableRelations = relations(localesTable, ({ one }) => ({\n _parentID: one(table, {\n fields: [localesTable._parentID],\n references: [table.id],\n }),\n }))\n\n adapter.relations[`relations_${localeTableName}`] = localesTableRelations\n }\n\n if (hasManyTextField && buildTexts) {\n const textsTableName = `${rootTableName}_texts`\n const columns: Record<string, PgColumnBuilder> = {\n id: serial('id').primaryKey(),\n order: integer('order').notNull(),\n parent: parentIDColumnMap[idColType]('parent_id').notNull(),\n path: varchar('path').notNull(),\n text: varchar('text'),\n }\n\n if (hasLocalizedManyTextField) {\n columns.locale = adapter.enums.enum__locales('locale')\n }\n\n textsTable = adapter.pgSchema.table(textsTableName, columns, (cols) => {\n const config: Record<string, ForeignKeyBuilder | IndexBuilder> = {\n orderParentIdx: index(`${textsTableName}_order_parent_idx`).on(cols.order, cols.parent),\n parentFk: foreignKey({\n name: `${textsTableName}_parent_fk`,\n columns: [cols.parent],\n foreignColumns: [table.id],\n }).onDelete('cascade'),\n }\n\n if (hasManyTextField === 'index') {\n config.text_idx = index(`${textsTableName}_text_idx`).on(cols.text)\n }\n\n if (hasLocalizedManyTextField) {\n config.localeParent = index(`${textsTableName}_locale_parent`).on(cols.locale, cols.parent)\n }\n\n return config\n })\n\n adapter.tables[textsTableName] = textsTable\n\n const textsTableRelations = relations(textsTable, ({ one }) => ({\n parent: one(table, {\n fields: [textsTable.parent],\n references: [table.id],\n }),\n }))\n\n adapter.relations[`relations_${textsTableName}`] = textsTableRelations\n }\n\n if (hasManyNumberField && buildNumbers) {\n const numbersTableName = `${rootTableName}_numbers`\n const columns: Record<string, PgColumnBuilder> = {\n id: serial('id').primaryKey(),\n number: numeric('number'),\n order: integer('order').notNull(),\n parent: parentIDColumnMap[idColType]('parent_id').notNull(),\n path: varchar('path').notNull(),\n }\n\n if (hasLocalizedManyNumberField) {\n columns.locale = adapter.enums.enum__locales('locale')\n }\n\n numbersTable = adapter.pgSchema.table(numbersTableName, columns, (cols) => {\n const config: Record<string, ForeignKeyBuilder | IndexBuilder> = {\n orderParentIdx: index(`${numbersTableName}_order_parent_idx`).on(cols.order, cols.parent),\n parentFk: foreignKey({\n name: `${numbersTableName}_parent_fk`,\n columns: [cols.parent],\n foreignColumns: [table.id],\n }).onDelete('cascade'),\n }\n\n if (hasManyNumberField === 'index') {\n config.numberIdx = index(`${numbersTableName}_number_idx`).on(cols.number)\n }\n\n if (hasLocalizedManyNumberField) {\n config.localeParent = index(`${numbersTableName}_locale_parent`).on(\n cols.locale,\n cols.parent,\n )\n }\n\n return config\n })\n\n adapter.tables[numbersTableName] = numbersTable\n\n const numbersTableRelations = relations(numbersTable, ({ one }) => ({\n parent: one(table, {\n fields: [numbersTable.parent],\n references: [table.id],\n }),\n }))\n\n adapter.relations[`relations_${numbersTableName}`] = numbersTableRelations\n }\n\n if (buildRelationships) {\n if (relationships.size) {\n const relationshipColumns: Record<string, PgColumnBuilder> = {\n id: serial('id').primaryKey(),\n order: integer('order'),\n parent: parentIDColumnMap[idColType]('parent_id').notNull(),\n path: varchar('path').notNull(),\n }\n\n if (hasLocalizedRelationshipField) {\n relationshipColumns.locale = adapter.enums.enum__locales('locale')\n }\n\n const relationExtraConfig: BaseExtraConfig = {}\n\n const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`\n\n relationships.forEach((relationTo) => {\n const relationshipConfig = adapter.payload.collections[relationTo].config\n const formattedRelationTo = getTableName({\n adapter,\n config: relationshipConfig,\n throwValidationError: true,\n })\n let colType = adapter.idType === 'uuid' ? 'uuid' : 'integer'\n\n const relatedCollectionCustomIDType =\n adapter.payload.collections[relationshipConfig.slug]?.customIDType\n\n if (relatedCollectionCustomIDType === 'number') colType = 'numeric'\n if (relatedCollectionCustomIDType === 'text') colType = 'varchar'\n\n relationshipColumns[`${relationTo}ID`] = parentIDColumnMap[colType](\n `${formattedRelationTo}_id`,\n )\n\n relationExtraConfig[`${relationTo}IdFk`] = (cols) =>\n foreignKey({\n name: `${relationshipsTableName}_${toSnakeCase(relationTo)}_fk`,\n columns: [cols[`${relationTo}ID`]],\n foreignColumns: [adapter.tables[formattedRelationTo].id],\n }).onDelete('cascade')\n })\n\n relationshipsTable = adapter.pgSchema.table(\n relationshipsTableName,\n relationshipColumns,\n (cols) => {\n const result: Record<string, ForeignKeyBuilder | IndexBuilder> = Object.entries(\n relationExtraConfig,\n ).reduce(\n (config, [key, func]) => {\n config[key] = func(cols)\n return config\n },\n {\n order: index(`${relationshipsTableName}_order_idx`).on(cols.order),\n parentFk: foreignKey({\n name: `${relationshipsTableName}_parent_fk`,\n columns: [cols.parent],\n foreignColumns: [table.id],\n }).onDelete('cascade'),\n parentIdx: index(`${relationshipsTableName}_parent_idx`).on(cols.parent),\n pathIdx: index(`${relationshipsTableName}_path_idx`).on(cols.path),\n },\n )\n\n if (hasLocalizedRelationshipField) {\n result.localeIdx = index(`${relationshipsTableName}_locale_idx`).on(cols.locale)\n }\n\n return result\n },\n )\n\n adapter.tables[relationshipsTableName] = relationshipsTable\n\n const relationshipsTableRelations = relations(relationshipsTable, ({ one }) => {\n const result: Record<string, Relation<string>> = {\n parent: one(table, {\n fields: [relationshipsTable.parent],\n references: [table.id],\n relationName: '_rels',\n }),\n }\n\n relationships.forEach((relationTo) => {\n const relatedTableName = getTableName({\n adapter,\n config: adapter.payload.collections[relationTo].config,\n throwValidationError: true,\n })\n const idColumnName = `${relationTo}ID`\n result[idColumnName] = one(adapter.tables[relatedTableName], {\n fields: [relationshipsTable[idColumnName]],\n references: [adapter.tables[relatedTableName].id],\n })\n })\n\n return result\n })\n\n adapter.relations[`relations_${relationshipsTableName}`] = relationshipsTableRelations\n }\n }\n\n const tableRelations = relations(table, ({ many }) => {\n const result: Record<string, Relation<string>> = {}\n\n relationsToBuild.forEach((val, key) => {\n result[key] = many(adapter.tables[val])\n })\n\n if (hasLocalizedField) {\n result._locales = many(localesTable)\n }\n\n if (hasManyTextField) {\n result._texts = many(textsTable)\n }\n if (hasManyNumberField) {\n result._numbers = many(numbersTable)\n }\n\n if (relationships.size && relationshipsTable) {\n result._rels = many(relationshipsTable, {\n relationName: '_rels',\n })\n }\n\n return result\n })\n\n adapter.relations[`relations_${tableName}`] = tableRelations\n\n return { hasManyNumberField, hasManyTextField, relationsToBuild }\n}\n"],"names":["relations","foreignKey","index","integer","numeric","serial","timestamp","unique","varchar","toSnakeCase","getTableName","parentIDColumnMap","setColumnID","traverseFields","buildTable","adapter","baseColumns","baseExtraConfig","buildNumbers","buildRelationships","buildTexts","disableNotNull","disableUnique","fields","rootRelationsToBuild","rootRelationships","rootTableIDColType","rootTableName","incomingRootTableName","tableName","timestamps","versions","columns","indexes","localesColumns","localesIndexes","localesTable","textsTable","numbersTable","relationships","Set","relationshipsTable","relationsToBuild","Map","idColType","hasLocalizedField","hasLocalizedManyNumberField","hasLocalizedManyTextField","hasLocalizedRelationshipField","hasManyNumberField","hasManyTextField","newTableName","parentTableName","createdAt","mode","precision","withTimezone","defaultNow","notNull","updatedAt","table","pgSchema","cols","extraConfig","Object","entries","reduce","config","key","func","result","acc","colName","tables","localeTableName","localesSuffix","id","primaryKey","_locale","enums","enum__locales","_parentID","_localeParent","on","_parentIdFk","name","foreignColumns","onDelete","localesTableRelations","one","references","textsTableName","order","parent","path","text","locale","orderParentIdx","parentFk","text_idx","localeParent","textsTableRelations","numbersTableName","number","numberIdx","numbersTableRelations","size","relationshipColumns","relationExtraConfig","relationshipsTableName","relationshipsSuffix","forEach","relationTo","relationshipConfig","payload","collections","formattedRelationTo","throwValidationError","colType","idType","relatedCollectionCustomIDType","slug","customIDType","parentIdx","pathIdx","localeIdx","relationshipsTableRelations","relationName","relatedTableName","idColumnName","tableRelations","many","val","_locales","_texts","_numbers","_rels"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,oCAAoC,GAWpC,SAASA,SAAS,QAAQ,cAAa;AACvC,SACEC,UAAU,EACVC,KAAK,EACLC,OAAO,EACPC,OAAO,EACPC,MAAM,EACNC,SAAS,EACTC,MAAM,EACNC,OAAO,QACF,sBAAqB;AAC5B,OAAOC,iBAAiB,gBAAe;AAI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AC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC/C,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,YAAY,CAAC,CAAA;IACtE,YAAY,EAAE,MAAM,CAAA;IACpB,eAAe,EAAE,MAAM,CAAA;IACvB,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACrC,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC1B,oBAAoB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC1C,kBAAkB,EAAE,MAAM,CAAA;IAC1B,aAAa,EAAE,MAAM,CAAA;IACrB,QAAQ,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,KAAK,MAAM,GAAG;IACZ,iBAAiB,EAAE,OAAO,CAAA;IAC1B,2BAA2B,EAAE,OAAO,CAAA;IACpC,yBAAyB,EAAE,OAAO,CAAA;IAClC,6BAA6B,EAAE,OAAO,CAAA;IACtC,kBAAkB,EAAE,OAAO,GAAG,OAAO,CAAA;IACrC,gBAAgB,EAAE,OAAO,GAAG,OAAO,CAAA;CACpC,CAAA;AAED,eAAO,MAAM,cAAc,sUAuBxB,IAAI,KAAG,MAqnBT,CAAA"}
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-param-reassign */ import { relations } from 'drizzle-orm';
2
- import { PgNumericBuilder, PgUUIDBuilder, PgVarcharBuilder, boolean, index, integer, jsonb, numeric, pgEnum, text, timestamp, varchar } from 'drizzle-orm/pg-core';
2
+ import { PgNumericBuilder, PgUUIDBuilder, PgVarcharBuilder, boolean, foreignKey, index, integer, jsonb, numeric, pgEnum, text, timestamp, varchar } from 'drizzle-orm/pg-core';
3
3
  import { InvalidConfiguration } from 'payload/errors';
4
4
  import { fieldAffectsData, optionIsObject } from 'payload/types';
5
5
  import toSnakeCase from 'to-snake-case';
@@ -136,8 +136,7 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
136
136
  parentTableName: newTableName,
137
137
  prefix: `enum_${newTableName}_`,
138
138
  target: 'enumName',
139
- throwValidationError,
140
- versions
139
+ throwValidationError
141
140
  });
142
141
  adapter.enums[enumName] = pgEnum(enumName, field.options.map((option)=>{
143
142
  if (optionIsObject(option)) {
@@ -151,18 +150,24 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
151
150
  config: field,
152
151
  parentTableName: newTableName,
153
152
  prefix: `${newTableName}_`,
154
- throwValidationError,
155
- versions
153
+ throwValidationError
156
154
  });
157
155
  const baseColumns = {
158
156
  order: integer('order').notNull(),
159
- parent: parentIDColumnMap[parentIDColType]('parent_id').references(()=>adapter.tables[parentTableName].id, {
160
- onDelete: 'cascade'
161
- }).notNull(),
157
+ parent: parentIDColumnMap[parentIDColType]('parent_id').notNull(),
162
158
  value: adapter.enums[enumName]('value')
163
159
  };
164
160
  const baseExtraConfig = {
165
161
  orderIdx: (cols)=>index(`${selectTableName}_order_idx`).on(cols.order),
162
+ parentFk: (cols)=>foreignKey({
163
+ name: `${selectTableName}_parent_fk`,
164
+ columns: [
165
+ cols.parent
166
+ ],
167
+ foreignColumns: [
168
+ adapter.tables[parentTableName].id
169
+ ]
170
+ }),
166
171
  parentIdx: (cols)=>index(`${selectTableName}_parent_idx`).on(cols.parent)
167
172
  };
168
173
  if (field.localized) {
@@ -219,12 +224,19 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
219
224
  });
220
225
  const baseColumns = {
221
226
  _order: integer('_order').notNull(),
222
- _parentID: parentIDColumnMap[parentIDColType]('_parent_id').references(()=>adapter.tables[parentTableName].id, {
223
- onDelete: 'cascade'
224
- }).notNull()
227
+ _parentID: parentIDColumnMap[parentIDColType]('_parent_id').notNull()
225
228
  };
226
229
  const baseExtraConfig = {
227
230
  _orderIdx: (cols)=>index(`${arrayTableName}_order_idx`).on(cols._order),
231
+ _parentIDFk: (cols)=>foreignKey({
232
+ name: `${arrayTableName}_parent_id_fk`,
233
+ columns: [
234
+ cols['_parentID']
235
+ ],
236
+ foreignColumns: [
237
+ adapter.tables[parentTableName].id
238
+ ]
239
+ }).onDelete('cascade'),
228
240
  _parentIDIdx: (cols)=>index(`${arrayTableName}_parent_id_idx`).on(cols._parentID)
229
241
  };
230
242
  if (field.localized && adapter.payload.config.localization) {
@@ -288,14 +300,21 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
288
300
  if (!adapter.tables[blockTableName]) {
289
301
  const baseColumns = {
290
302
  _order: integer('_order').notNull(),
291
- _parentID: parentIDColumnMap[rootTableIDColType]('_parent_id').references(()=>adapter.tables[rootTableName].id, {
292
- onDelete: 'cascade'
293
- }).notNull(),
303
+ _parentID: parentIDColumnMap[rootTableIDColType]('_parent_id').notNull(),
294
304
  _path: text('_path').notNull()
295
305
  };
296
306
  const baseExtraConfig = {
297
307
  _orderIdx: (cols)=>index(`${blockTableName}_order_idx`).on(cols._order),
298
308
  _parentIDIdx: (cols)=>index(`${blockTableName}_parent_id_idx`).on(cols._parentID),
309
+ _parentIdFk: (cols)=>foreignKey({
310
+ name: `${blockTableName}_parent_id_fk`,
311
+ columns: [
312
+ cols._parentID
313
+ ],
314
+ foreignColumns: [
315
+ adapter.tables[rootTableName].id
316
+ ]
317
+ }).onDelete('cascade'),
299
318
  _pathIdx: (cols)=>index(`${blockTableName}_path_idx`).on(cols._path)
300
319
  };
301
320
  if (field.localized && adapter.payload.config.localization) {
@@ -347,7 +366,8 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
347
366
  block,
348
367
  localized: field.localized,
349
368
  rootTableName,
350
- table: adapter.tables[blockTableName]
369
+ table: adapter.tables[blockTableName],
370
+ tableLocales: adapter.tables[`${blockTableName}${adapter.localesSuffix}`]
351
371
  });
352
372
  }
353
373
  adapter.blockTableNames[`${rootTableName}.${toSnakeCase(block.slug)}`] = blockTableName;
@@ -481,7 +501,7 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
481
501
  indexes,
482
502
  localesColumns,
483
503
  localesIndexes,
484
- newTableName: parentTableName,
504
+ newTableName,
485
505
  parentTableName,
486
506
  relationsToBuild,
487
507
  relationships,