This commit is contained in:
Adria Navarro 2023-10-11 13:48:28 +02:00
parent a3b3c176d3
commit 514f2b0cef
2 changed files with 20 additions and 23 deletions

View File

@ -568,18 +568,14 @@ export class ExternalRequest<T extends Operation> {
// need to specify where to put this back into // need to specify where to put this back into
column: fieldName, column: fieldName,
} }
if ((field as ManyToManyRelationshipFieldMetadata).through) { if (isManyToMany(field)) {
const { tableName: throughTableName } = breakExternalTableId( const { tableName: throughTableName } = breakExternalTableId(
(field as ManyToManyRelationshipFieldMetadata).through field.through
) )
definition.through = throughTableName definition.through = throughTableName
// don't support composite keys for relationships // don't support composite keys for relationships
definition.from = definition.from = field.throughTo || table.primary[0]
(field as ManyToManyRelationshipFieldMetadata).throughTo || definition.to = field.throughFrom || linkTable.primary[0]
table.primary[0]
definition.to =
(field as ManyToManyRelationshipFieldMetadata).throughFrom ||
linkTable.primary[0]
definition.fromPrimary = table.primary[0] definition.fromPrimary = table.primary[0]
definition.toPrimary = linkTable.primary[0] definition.toPrimary = linkTable.primary[0]
} }
@ -603,7 +599,7 @@ export class ExternalRequest<T extends Operation> {
const primaryKey = table.primary[0] const primaryKey = table.primary[0]
// make a new request to get the row with all its relationships // make a new request to get the row with all its relationships
// we need this to work out if any relationships need removed // we need this to work out if any relationships need removed
for (let field of Object.values(table.schema)) { for (const field of Object.values(table.schema)) {
if ( if (
field.type !== FieldTypes.LINK || field.type !== FieldTypes.LINK ||
!field.fieldName || !field.fieldName ||
@ -612,16 +608,13 @@ export class ExternalRequest<T extends Operation> {
continue continue
} }
const isMany = field.relationshipType === RelationshipType.MANY_TO_MANY const isMany = field.relationshipType === RelationshipType.MANY_TO_MANY
const tableId = isMany const tableId = isMany ? field.through : field.tableId
? (field as ManyToManyRelationshipFieldMetadata).through
: field.tableId
const { tableName: relatedTableName } = breakExternalTableId(tableId) const { tableName: relatedTableName } = breakExternalTableId(tableId)
// @ts-ignore // @ts-ignore
const linkPrimaryKey = this.tables[relatedTableName].primary[0] const linkPrimaryKey = this.tables[relatedTableName].primary[0]
const manyKey =
(field as ManyToManyRelationshipFieldMetadata).throughTo || primaryKey
const lookupField = isMany ? primaryKey : (field as any).foreignKey const lookupField = isMany ? primaryKey : (field as any).foreignKey
const fieldName = isMany ? manyKey : field.fieldName const fieldName = isMany ? field.throughTo || primaryKey : field.fieldName
if (!lookupField || !row[lookupField]) { if (!lookupField || !row[lookupField]) {
continue continue
} }

View File

@ -81,7 +81,9 @@ function cleanupRelationships(
const relatedTable = Object.values(tables).find( const relatedTable = Object.values(tables).find(
table => table._id === schemaTableId table => table._id === schemaTableId
) )
const foreignKey = (schema as any).foreignKey const foreignKey =
schema.relationshipType !== RelationshipType.MANY_TO_MANY &&
schema.foreignKey
if (!relatedTable || !foreignKey) { if (!relatedTable || !foreignKey) {
continue continue
} }
@ -183,17 +185,19 @@ function generateRelatedSchema(
) { ) {
// generate column for other table // generate column for other table
const relatedSchema = cloneDeep(linkColumn) const relatedSchema = cloneDeep(linkColumn)
const isMany2Many =
linkColumn.relationshipType === RelationshipType.MANY_TO_MANY
// swap them from the main link // swap them from the main link
if ((linkColumn as any).foreignKey) { if (!isMany2Many && linkColumn.foreignKey) {
relatedSchema.fieldName = (linkColumn as any).foreignKey relatedSchema.fieldName = linkColumn.foreignKey
relatedSchema.foreignKey = linkColumn.fieldName relatedSchema.foreignKey = linkColumn.fieldName
} }
// is many to many // is many to many
else { else if (isMany2Many) {
// don't need to copy through, already got it // don't need to copy through, already got it
relatedSchema.fieldName = (linkColumn as any).throughTo relatedSchema.fieldName = linkColumn.throughTo
relatedSchema.throughTo = (linkColumn as any).throughFrom relatedSchema.throughTo = linkColumn.throughFrom
relatedSchema.throughFrom = (linkColumn as any).throughTo relatedSchema.throughFrom = linkColumn.throughTo
} }
relatedSchema.relationshipType = otherRelationshipType( relatedSchema.relationshipType = otherRelationshipType(
linkColumn.relationshipType linkColumn.relationshipType
@ -203,7 +207,7 @@ function generateRelatedSchema(
table.schema[columnName] = relatedSchema table.schema[columnName] = relatedSchema
} }
function isRelationshipSetup(column: FieldSchema) { function isRelationshipSetup(column: RelationshipFieldMetadata) {
return (column as any).foreignKey || (column as any).through return (column as any).foreignKey || (column as any).through
} }