Fixing some issues with MySQL and dropping foreign key constrained columns.

This commit is contained in:
mike12345567 2021-11-02 13:36:23 +00:00
parent 5d40882c66
commit 953c90c8d4
4 changed files with 20 additions and 1 deletions

View File

@ -98,6 +98,7 @@ function generateManyLinkSchema(datasource, column, table, relatedTable) {
_id: buildExternalTableId(datasource._id, jcTblName), _id: buildExternalTableId(datasource._id, jcTblName),
name: jcTblName, name: jcTblName,
primary: [primary, relatedPrimary], primary: [primary, relatedPrimary],
constrained: [primary, relatedPrimary],
schema: { schema: {
[primary]: foreignKeyStructure(primary, { [primary]: foreignKeyStructure(primary, {
toTable: table.name, toTable: table.name,
@ -210,6 +211,12 @@ exports.save = async function (ctx) {
relationType relationType
) )
fkTable.schema[foreignKey] = foreignKeyStructure(foreignKey) fkTable.schema[foreignKey] = foreignKeyStructure(foreignKey)
if (fkTable.constrained == null) {
fkTable.constrained = []
}
if (fkTable.constrained.indexOf(foreignKey) === -1) {
fkTable.constrained.push(foreignKey)
}
// foreign key is in other table, need to save it to external // foreign key is in other table, need to save it to external
if (fkTable._id !== table._id) { if (fkTable._id !== table._id) {
extraTablesToUpdate.push(fkTable) extraTablesToUpdate.push(fkTable)
@ -234,6 +241,13 @@ exports.save = async function (ctx) {
await makeTableRequest(datasource, op, extraTable, tables, oldExtraTable) await makeTableRequest(datasource, op, extraTable, tables, oldExtraTable)
} }
// make sure the constrained list, all still exist
if (Array.isArray(tableToSave.constrained)) {
tableToSave.constrained = tableToSave.constrained.filter(constraint =>
Object.keys(tableToSave.schema).includes(constraint)
)
}
// store it into couch now for budibase reference // store it into couch now for budibase reference
datasource.entities[tableToSave.name] = tableToSave datasource.entities[tableToSave.name] = tableToSave
await db.put(datasource) await db.put(datasource)

View File

@ -46,6 +46,7 @@ export interface Table extends Base {
schema: TableSchema schema: TableSchema
primaryDisplay?: string primaryDisplay?: string
sourceId?: string sourceId?: string
constrained?: string[]
} }
export interface Row extends Base { export interface Row extends Base {

View File

@ -19,6 +19,7 @@ function generateSchema(schema: CreateTableBuilder, table: Table, tables: Record
schema.primary(metaCols.map(col => col.name)) schema.primary(metaCols.map(col => col.name))
} }
// check if any columns need added // check if any columns need added
const foreignKeys = Object.values(table.schema).map(col => col.foreignKey) const foreignKeys = Object.values(table.schema).map(col => col.foreignKey)
for (let [key, column] of Object.entries(table.schema)) { for (let [key, column] of Object.entries(table.schema)) {
@ -78,6 +79,9 @@ function generateSchema(schema: CreateTableBuilder, table: Table, tables: Record
.filter(([key, schema]) => schema.type !== FieldTypes.LINK && table.schema[key] == null) .filter(([key, schema]) => schema.type !== FieldTypes.LINK && table.schema[key] == null)
.map(([key]) => key) .map(([key]) => key)
deletedColumns.forEach(key => { deletedColumns.forEach(key => {
if (oldTable.constrained && oldTable.constrained.indexOf(key) !== -1) {
schema.dropForeign(key)
}
schema.dropColumn(key) schema.dropColumn(key)
}) })
} }

View File

@ -265,7 +265,7 @@ module MySQLModule {
if (Array.isArray(input)) { if (Array.isArray(input)) {
const responses = [] const responses = []
for (let query of input) { for (let query of input) {
responses.push(await internalQuery(this.client, query)) responses.push(await internalQuery(this.client, query, false))
} }
return responses return responses
} }