Skip to content

Commit

Permalink
Fixed an issue where user was not able to assign new/old columns as p…
Browse files Browse the repository at this point in the history
…rimary key once column with primary key is deleted. #5749
  • Loading branch information
pravesh-sharma authored Oct 11, 2023
1 parent fc411bf commit 73430a2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default class ColumnSchema extends BaseUISchema {
// Need to show this field only when creating new table
// [in SubNode control]
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: ()=>{
return obj.top?.nodeInfo && _.isUndefined(
obj.top.nodeInfo['table'] || obj.top.nodeInfo['view'] ||
Expand All @@ -178,13 +178,13 @@ export default class ColumnSchema extends BaseUISchema {
// - Table is a partitioned table
if (
obj.top && ((
!_.isUndefined(obj.top.origData['oid'])
&& !_.isUndefined(obj.top.origData['primary_key'])
&& obj.top.origData['primary_key'].length > 0
&& !_.isUndefined(obj.top.origData['primary_key'][0]['oid'])
!_.isUndefined(obj.top.sessData['oid'])
&& !_.isUndefined(obj.top.sessData['primary_key'])
&& obj.top.sessData['primary_key'].length > 0
&& !_.isUndefined(obj.top.sessData['primary_key'][0]['oid'])
) || (
'is_partitioned' in obj.top.origData
&& obj.top.origData['is_partitioned']
'is_partitioned' in obj.top.sessData
&& obj.top.sessData['is_partitioned']
&& obj.getServerVersion() < 11000
))
) {
Expand All @@ -200,10 +200,10 @@ export default class ColumnSchema extends BaseUISchema {
// If primary key already exist then disable.
if (
obj.top && (
!_.isUndefined(obj.top.origData['oid'])
&& !_.isUndefined(obj.top.origData['primary_key'])
&& obj.top.origData['primary_key'].length > 0
&& !_.isUndefined(obj.top.origData['primary_key'][0]['oid'])
!_.isUndefined(obj.top.sessData['oid'])
&& !_.isUndefined(obj.top.sessData['primary_key'])
&& obj.top.sessData['primary_key'].length > 0
&& !_.isUndefined(obj.top.sessData['primary_key'][0]['oid'])
)
) {
return false;
Expand All @@ -212,8 +212,8 @@ export default class ColumnSchema extends BaseUISchema {
// If table is partitioned table then disable
if(
obj.top && (
'is_partitioned' in obj.top.origData
&& obj.top.origData['is_partitioned']
'is_partitioned' in obj.top.sessData
&& obj.top.sessData['is_partitioned']
&& obj.getServerVersion() < 11000)
) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class ConstraintsSchema extends BaseUISchema {
schema: this.primaryKeyObj,
editable: false, type: 'collection',
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'],
disabled: this.inCatalog,
canAdd: function(state) {
Expand All @@ -136,6 +136,20 @@ export class ConstraintsSchema extends BaseUISchema {
}));
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: '',
Expand Down
4 changes: 2 additions & 2 deletions web/pgadmin/static/js/SchemaView/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,6 @@ function SchemaDialogView({
useEffect(()=>{
/* if sessData changes, validate the schema */
if(!formReady) return;
/* Set the _sessData, can be usefull to some deep controls */
schema._sessData = sessData;
let isNotValid = validateSchema(schema, sessData, (path, message)=>{
if(message) {
setFormErr({
Expand Down Expand Up @@ -779,6 +777,8 @@ function SchemaDialogView({
};

let ButtonIcon = getButtonIcon();
/* Set the _sessData, can be usefull to some deep controls */
schema._sessData = sessData;

/* I am Groot */
return (
Expand Down

0 comments on commit 73430a2

Please sign in to comment.