Browse Source

fix: keep foreign key name in LTAR table

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5030/head
Pranav C 2 years ago
parent
commit
6bd05b0aa2
  1. 2
      packages/nc-gui/assets/style.scss
  2. 16
      packages/nocodb/src/lib/meta/api/columnApis.ts
  3. 5
      packages/nocodb/src/lib/meta/api/metaDiffApis.ts

2
packages/nc-gui/assets/style.scss

@ -308,7 +308,7 @@ a {
.ant-btn-loading-icon{ .ant-btn-loading-icon{
& > span { & > span {
@apply block bg-red-500 @apply block;
} }
} }

16
packages/nocodb/src/lib/meta/api/columnApis.ts

@ -51,7 +51,7 @@ export enum Altered {
} }
// generate unique foreign key constraint name for foreign key // generate unique foreign key constraint name for foreign key
const generateFkConstraintName = (parent: TableType, child: TableType) => { const generateFkName = (parent: TableType, child: TableType) => {
// generate a unique constraint name by taking first 10 chars of parent and child table name (by replacing all non word chars with _) // generate a unique constraint name by taking first 10 chars of parent and child table name (by replacing all non word chars with _)
// and appending a random string of 15 chars maximum length. // and appending a random string of 15 chars maximum length.
// In database constraint name can be upto 64 chars and here we are generating a name of maximum 40 chars // In database constraint name can be upto 64 chars and here we are generating a name of maximum 40 chars
@ -93,7 +93,7 @@ async function createHmAndBtColumn(
fk_related_model_id: parent.id, fk_related_model_id: parent.id,
virtual, virtual,
system: isSystemCol, system: isSystemCol,
fk_col_name: fkColName, fk_index_name: fkColName,
}); });
} }
// save hm column // save hm column
@ -112,7 +112,7 @@ async function createHmAndBtColumn(
fk_related_model_id: child.id, fk_related_model_id: child.id,
virtual, virtual,
system: isSystemCol, system: isSystemCol,
fk_col_name: fkColName, fk_index_name: fkColName,
}); });
} }
} }
@ -278,6 +278,7 @@ export async function columnAdd(
`${parent.table_name}_id` `${parent.table_name}_id`
); );
let foreignKeyName;
{ {
// create foreign key // create foreign key
const newColumn = { const newColumn = {
@ -321,10 +322,9 @@ export async function columnAdd(
childColumn = await Column.get({ colId: id }); childColumn = await Column.get({ colId: id });
let foreignKeyName;
// ignore relation creation if virtual // ignore relation creation if virtual
if (!(req.body as LinkToAnotherColumnReqType).virtual) { if (!(req.body as LinkToAnotherColumnReqType).virtual) {
foreignKeyName = generateFkConstraintName(parent, child); foreignKeyName = generateFkName(parent, child);
// create relation // create relation
await sqlMgr.sqlOpPlus(base, 'relationCreate', { await sqlMgr.sqlOpPlus(base, 'relationCreate', {
childColumn: fkColName, childColumn: fkColName,
@ -357,7 +357,7 @@ export async function columnAdd(
childColumn, childColumn,
(req.body as LinkToAnotherColumnReqType).type as RelationTypes, (req.body as LinkToAnotherColumnReqType).type as RelationTypes,
(req.body as LinkToAnotherColumnReqType).title, (req.body as LinkToAnotherColumnReqType).title,
fkColName, foreignKeyName,
(req.body as LinkToAnotherColumnReqType).virtual (req.body as LinkToAnotherColumnReqType).virtual
); );
} else if ((req.body as LinkToAnotherColumnReqType).type === 'mm') { } else if ((req.body as LinkToAnotherColumnReqType).type === 'mm') {
@ -423,8 +423,8 @@ export async function columnAdd(
let foreignKeyName2; let foreignKeyName2;
if (!(req.body as LinkToAnotherColumnReqType).virtual) { if (!(req.body as LinkToAnotherColumnReqType).virtual) {
foreignKeyName1 = generateFkConstraintName(parent, child); foreignKeyName1 = generateFkName(parent, child);
foreignKeyName2 = generateFkConstraintName(parent, child); foreignKeyName2 = generateFkName(parent, child);
const rel1Args = { const rel1Args = {
...req.body, ...req.body,

5
packages/nocodb/src/lib/meta/api/metaDiffApis.ts

@ -107,6 +107,7 @@ type MetaDiffChange = {
cn?: string; cn?: string;
rcn?: string; rcn?: string;
relationType: RelationTypes; relationType: RelationTypes;
cstn?: string;
} }
); );
@ -146,6 +147,7 @@ async function getMetaDiff(
cn: string; cn: string;
rcn: string; rcn: string;
found?: any; found?: any;
cstn?: string;
}> = (await sqlClient.relationListAll())?.data?.list; }> = (await sqlClient.relationListAll())?.data?.list;
for (const table of tableList) { for (const table of tableList) {
@ -394,6 +396,7 @@ async function getMetaDiff(
rcn: relation.rcn, rcn: relation.rcn,
msg: `New relation added`, msg: `New relation added`,
relationType: RelationTypes.BELONGS_TO, relationType: RelationTypes.BELONGS_TO,
cstn: relation.cstn,
}); });
} }
if (!relation?.found?.[RelationTypes.HAS_MANY]) { if (!relation?.found?.[RelationTypes.HAS_MANY]) {
@ -736,6 +739,7 @@ export async function metaDiffSync(req, res) {
fk_parent_column_id: parentCol.id, fk_parent_column_id: parentCol.id,
fk_child_column_id: childCol.id, fk_child_column_id: childCol.id,
virtual: false, virtual: false,
fk_index_name: change.cstn,
}); });
} else if (change.relationType === RelationTypes.HAS_MANY) { } else if (change.relationType === RelationTypes.HAS_MANY) {
const title = getUniqueColumnAliasName( const title = getUniqueColumnAliasName(
@ -751,6 +755,7 @@ export async function metaDiffSync(req, res) {
fk_parent_column_id: parentCol.id, fk_parent_column_id: parentCol.id,
fk_child_column_id: childCol.id, fk_child_column_id: childCol.id,
virtual: false, virtual: false,
fk_index_name: change.cstn,
}); });
} }
}); });

Loading…
Cancel
Save