Merge pull request #2539 from Budibase/fix/table-primary-same-name

Fixing issue with relationships that was uncovered by having same primary display and table name
This commit is contained in:
Michael Drury 2021-09-03 15:44:59 +01:00 committed by GitHub
commit aafd49ce82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 17 deletions

View File

@ -112,16 +112,24 @@ export const enrichRows = async (rows, tableId) => {
if (!Array.isArray(rows)) { if (!Array.isArray(rows)) {
return [] return []
} }
if (rows.length && tableId) { if (rows.length) {
// Fetch table schema so we can check column types // map of tables, incase a row being loaded is not from the same table
const tableDefinition = await fetchTableDefinition(tableId) const tables = {}
const schema = tableDefinition && tableDefinition.schema for (let row of rows) {
if (schema) { // fallback to passed in tableId if row doesn't have it specified
const keys = Object.keys(schema) let rowTableId = row.tableId || tableId
rows.forEach(row => { let table = tables[rowTableId]
if (!table) {
// Fetch table schema so we can check column types
table = await fetchTableDefinition(rowTableId)
tables[rowTableId] = table
}
const schema = table?.schema
if (schema) {
const keys = Object.keys(schema)
for (let key of keys) { for (let key of keys) {
const type = schema[key].type const type = schema[key].type
if (type === "link") { if (type === "link" && Array.isArray(row[key])) {
// Enrich row a string join of relationship fields // Enrich row a string join of relationship fields
row[`${key}_text`] = row[`${key}_text`] =
row[key] row[key]
@ -137,7 +145,7 @@ export const enrichRows = async (rows, tableId) => {
row[`${key}_first`] = url row[`${key}_first`] = url
} }
} }
}) }
} }
} }
return rows return rows

View File

@ -203,19 +203,17 @@ exports.attachFullLinkedDocs = async (ctx, table, rows) => {
exports.squashLinksToPrimaryDisplay = async (appId, table, enriched) => { exports.squashLinksToPrimaryDisplay = async (appId, table, enriched) => {
const db = new CouchDB(appId) const db = new CouchDB(appId)
// will populate this as we find them // will populate this as we find them
const linkedTables = [] const linkedTables = [table]
for (let [column, schema] of Object.entries(table.schema)) { for (let row of enriched) {
if (schema.type !== FieldTypes.LINK) { // this only fetches the table if its not already in array
continue const rowTable = await getLinkedTable(db, row.tableId, linkedTables)
} for (let [column, schema] of Object.entries(rowTable.schema)) {
for (let row of enriched) { if (schema.type !== FieldTypes.LINK || !Array.isArray(row[column])) {
if (!row[column] || !row[column].length) {
continue continue
} }
const newLinks = [] const newLinks = []
for (let link of row[column]) { for (let link of row[column]) {
const linkTblId = link.tableId || getRelatedTableForField(table, column) const linkTblId = link.tableId || getRelatedTableForField(table, column)
// this only fetches the table if its not already in array
const linkedTable = await getLinkedTable(db, linkTblId, linkedTables) const linkedTable = await getLinkedTable(db, linkTblId, linkedTables)
const obj = { _id: link._id } const obj = { _id: link._id }
if (link[linkedTable.primaryDisplay]) { if (link[linkedTable.primaryDisplay]) {