1
0
Fork 0
mirror of synced 2024-07-13 18:26:06 +12:00
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
column: fieldName,
}
if ((field as ManyToManyRelationshipFieldMetadata).through) {
if (isManyToMany(field)) {
const { tableName: throughTableName } = breakExternalTableId(
(field as ManyToManyRelationshipFieldMetadata).through
field.through
)
definition.through = throughTableName
// don't support composite keys for relationships
definition.from =
(field as ManyToManyRelationshipFieldMetadata).throughTo ||
table.primary[0]
definition.to =
(field as ManyToManyRelationshipFieldMetadata).throughFrom ||
linkTable.primary[0]
definition.from = field.throughTo || table.primary[0]
definition.to = field.throughFrom || linkTable.primary[0]
definition.fromPrimary = table.primary[0]
definition.toPrimary = linkTable.primary[0]
}
@ -603,7 +599,7 @@ export class ExternalRequest<T extends Operation> {
const primaryKey = table.primary[0]
// make a new request to get the row with all its relationships
// 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 (
field.type !== FieldTypes.LINK ||
!field.fieldName ||
@ -612,16 +608,13 @@ export class ExternalRequest<T extends Operation> {
continue
}
const isMany = field.relationshipType === RelationshipType.MANY_TO_MANY
const tableId = isMany
? (field as ManyToManyRelationshipFieldMetadata).through
: field.tableId
const tableId = isMany ? field.through : field.tableId
const { tableName: relatedTableName } = breakExternalTableId(tableId)
// @ts-ignore
const linkPrimaryKey = this.tables[relatedTableName].primary[0]
const manyKey =
(field as ManyToManyRelationshipFieldMetadata).throughTo || primaryKey
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]) {
continue
}

View file

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