Fixed an issue where user was not able to assign new/old columns as primary key once column with primary key is deleted. #5749

This commit is contained in:
Pravesh Sharma 2023-10-11 13:29:19 +05:30 committed by GitHub
parent fc411bfc49
commit 73430a2062
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 16 deletions

View File

@ -165,7 +165,7 @@ export default class ColumnSchema extends BaseUISchema {
// Need to show this field only when creating new table // Need to show this field only when creating new table
// [in SubNode control] // [in SubNode control]
id: 'is_primary_key', label: gettext('Primary key?'), id: 'is_primary_key', label: gettext('Primary key?'),
cell: 'switch', type: 'switch', width: 100, disableResizing: true, deps:['name'], cell: 'switch', type: 'switch', width: 100, disableResizing: true, deps:['name', ['primary_key']],
visible: ()=>{ visible: ()=>{
return obj.top?.nodeInfo && _.isUndefined( return obj.top?.nodeInfo && _.isUndefined(
obj.top.nodeInfo['table'] || obj.top.nodeInfo['view'] || obj.top.nodeInfo['table'] || obj.top.nodeInfo['view'] ||
@ -178,13 +178,13 @@ export default class ColumnSchema extends BaseUISchema {
// - Table is a partitioned table // - Table is a partitioned table
if ( if (
obj.top && (( obj.top && ((
!_.isUndefined(obj.top.origData['oid']) !_.isUndefined(obj.top.sessData['oid'])
&& !_.isUndefined(obj.top.origData['primary_key']) && !_.isUndefined(obj.top.sessData['primary_key'])
&& obj.top.origData['primary_key'].length > 0 && obj.top.sessData['primary_key'].length > 0
&& !_.isUndefined(obj.top.origData['primary_key'][0]['oid']) && !_.isUndefined(obj.top.sessData['primary_key'][0]['oid'])
) || ( ) || (
'is_partitioned' in obj.top.origData 'is_partitioned' in obj.top.sessData
&& obj.top.origData['is_partitioned'] && obj.top.sessData['is_partitioned']
&& obj.getServerVersion() < 11000 && obj.getServerVersion() < 11000
)) ))
) { ) {
@ -200,10 +200,10 @@ export default class ColumnSchema extends BaseUISchema {
// If primary key already exist then disable. // If primary key already exist then disable.
if ( if (
obj.top && ( obj.top && (
!_.isUndefined(obj.top.origData['oid']) !_.isUndefined(obj.top.sessData['oid'])
&& !_.isUndefined(obj.top.origData['primary_key']) && !_.isUndefined(obj.top.sessData['primary_key'])
&& obj.top.origData['primary_key'].length > 0 && obj.top.sessData['primary_key'].length > 0
&& !_.isUndefined(obj.top.origData['primary_key'][0]['oid']) && !_.isUndefined(obj.top.sessData['primary_key'][0]['oid'])
) )
) { ) {
return false; return false;
@ -212,8 +212,8 @@ export default class ColumnSchema extends BaseUISchema {
// If table is partitioned table then disable // If table is partitioned table then disable
if( if(
obj.top && ( obj.top && (
'is_partitioned' in obj.top.origData 'is_partitioned' in obj.top.sessData
&& obj.top.origData['is_partitioned'] && obj.top.sessData['is_partitioned']
&& obj.getServerVersion() < 11000) && obj.getServerVersion() < 11000)
) { ) {
return false; return false;

View File

@ -113,7 +113,7 @@ export class ConstraintsSchema extends BaseUISchema {
schema: this.primaryKeyObj, schema: this.primaryKeyObj,
editable: false, type: 'collection', editable: false, type: 'collection',
group: gettext('Primary Key'), mode: ['edit', 'create'], group: gettext('Primary Key'), mode: ['edit', 'create'],
canEdit: true, canDelete: true, deps:['is_partitioned', 'typname'], canEdit: true, canDelete: true, deps:['is_partitioned', 'typname', 'columns'],
columns : ['name', 'columns'], columns : ['name', 'columns'],
disabled: this.inCatalog, disabled: this.inCatalog,
canAdd: function(state) { canAdd: function(state) {
@ -136,6 +136,20 @@ export class ConstraintsSchema extends BaseUISchema {
})); }));
return {columns: state.columns}; return {columns: state.columns};
} }
/* If column or primary key is deleted */
if(actionObj.type === SCHEMA_STATE_ACTIONS.DELETE_ROW) {
let deletedColumn = _.differenceBy(actionObj.oldState.columns,state.columns,'cid');
if(deletedColumn.length && deletedColumn[0].is_primary_key && !obj.top.isNew(state)) {
state.columns = state.columns.map(c=>({
...c, is_primary_key: false
}));
return {primary_key: []};
} else if(source[0] === 'primary_key') {
state.columns = state.columns.map(c=>({
...c, is_primary_key: false
}));
}
}
} }
},{ },{
id: 'foreign_key', label: '', id: 'foreign_key', label: '',

View File

@ -515,8 +515,6 @@ function SchemaDialogView({
useEffect(()=>{ useEffect(()=>{
/* if sessData changes, validate the schema */ /* if sessData changes, validate the schema */
if(!formReady) return; if(!formReady) return;
/* Set the _sessData, can be usefull to some deep controls */
schema._sessData = sessData;
let isNotValid = validateSchema(schema, sessData, (path, message)=>{ let isNotValid = validateSchema(schema, sessData, (path, message)=>{
if(message) { if(message) {
setFormErr({ setFormErr({
@ -779,6 +777,8 @@ function SchemaDialogView({
}; };
let ButtonIcon = getButtonIcon(); let ButtonIcon = getButtonIcon();
/* Set the _sessData, can be usefull to some deep controls */
schema._sessData = sessData;
/* I am Groot */ /* I am Groot */
return ( return (