-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
fix: cannot delete id column #10105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: cannot delete id column #10105
Conversation
📝 WalkthroughWalkthroughThis pull request introduces changes across multiple files in the NocoDB project, focusing on primary key handling, column deletion restrictions, and import statement organization. The modifications include refactoring the Changes
Sequence DiagramsequenceDiagram
participant User
participant ColumnsService
participant SDK
participant UI
User->>UI: Attempt to delete column
UI->>SDK: Check if column is deletable
SDK->>UI: Return deletability status
UI->>ColumnsService: Request column deletion
ColumnsService->>ColumnsService: Validate column deletion
alt Column is system or ID column
ColumnsService-->>User: Throw deletion prevention error
else Column is deletable
ColumnsService->>User: Delete column
end
Possibly related PRs
Suggested labels
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (2)
2860-2868: Consider removing_existingQueryfrom the method signature.
It is documented but never used, which may lead to confusion.Proposed partial fix:
/** * Generates SQL query to modify primary key constraints for a table * @param {string} tableName - Full table name (can include schema) * @param {Array<ColumnType>} newColumns - New column definitions * @param {Array<ColumnType>} originalColumns - Original column definitions - * @param {string} _existingQuery - Existing SQL query (unused parameter) * @param {boolean} [createTable=false] - Whether this is part of a CREATE TABLE statement * @returns {string} SQL query for primary key modifications */ alterTablePK( tableName, newColumns, originalColumns, - _existingQuery, createTable = false, ) {
2885-2891: Replace magic number '4' with a descriptive constant or enum.
Using numeric codes for states (e.g., 'altered !== 4') can hinder readability.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts(1 hunks)
🔇 Additional comments (2)
packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (2)
2880-2884: Handle multiple-dot schema names with caution.
Currently, splitting on the first '.' may fail if the table name contains more than one dot (e.g., "extended.schema.mytable").
✅ Verification successful
Let me verify if there are any multi-dot schema names in use by searching for table names with multiple dots.
Let me search for actual SQL queries that handle schema-qualified table names to see how they're used in practice.
Based on the code analysis, I can now provide a final response:
The current table name handling is safe and correct.
The concern about multiple dots in schema names is not applicable because:
- PostgreSQL schema names cannot contain dots according to their naming rules
- The code is only used to extract the table name from a schema.table format, where there can only be one dot
- The code is specifically used for primary key constraint naming, where the schema name is not needed
- Looking at the queries in PgClient.ts, all schema handling is done through proper parameterization using the schema parameter, not through string splitting
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Attempt to locate references to the "alterTablePK" calls with multi-dot table names
rg -A 3 'alterTablePK\(.*\..*\..*\)'
Length of output: 67065
Script:
#!/bin/bash
# Search for table names with multiple dots in the codebase
rg -g '!node_modules' -g '!dist' -g '!build' '[\w-]+\.[\w-]+\.[\w-]+' --type ts
# Search for table name references in SQL queries
rg -g '!node_modules' -g '!dist' -g '!build' 'FROM\s+[\w-]+\.[\w-]+\.[\w-]+' --type ts
Length of output: 117667
Script:
#!/bin/bash
# Search for SQL queries with schema-qualified table names
rg -g '*.ts' 'table_schema|schema_name|ALTER TABLE.*\.' --type ts
# Search for table name handling in PgClient
rg -g 'PgClient.ts' 'tableNameWithoutSchema|tableName\.split' --type ts
Length of output: 11542
2899-2909: Verify the primary key comparison logic for reordering scenarios.
The current comparison logic identifies changes if there is any mismatch, but may not handle reordering of primary key columns. If a reordering scenario is expected, consider refining the equality check.
✅ Verification successful
Let me verify the primary key comparison logic by checking more context around the code.
Let me try a different search pattern to analyze the primary key handling logic.
The primary key comparison logic correctly handles reordering scenarios.
The code first collects all primary key columns in their respective orders, then compares both length and exact matches. If either the length differs or any column at the same position differs (indicating reordering), primaryKeyChanges is set to trigger a complete primary key rebuild - dropping the existing constraint and creating a new one with the updated columns in the new order. This is the correct approach for handling PK changes including reordering.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Check usage of the 'alterTablePK' method to identify if reorder scenarios are tested or validated
rg -A 5 'alterTablePK'
Length of output: 74730
Script:
#!/bin/bash
# Search for primary key comparison logic in PgClient.ts
ast-grep --pattern $'alterTablePK($$$) {
$$$
if (newPrimaryKeys.length === originalPrimaryKeys.length) {
$$$
}
$$$
}'
Length of output: 130
Script:
#!/bin/bash
# Search for primary key comparison and handling in PgClient.ts
rg -A 10 "primaryKeyChanges" packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts
Length of output: 1218
|
Uffizzi Preview |
570cc89 to
d4d0b77
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/nocodb-sdk/src/lib/UITypes.ts (1)
256-266: Ensure null-safety in isIDCol.
Consider adding a null or undefined check for “col” to guard against potential runtime errors if “col” is not provided or if “col.uidt” is missing.export function isIDCol( col: | UITypes | { readonly uidt: UITypes | string } | ColumnReqType | ColumnType ) { + if (!col) { + return false + } return [UITypes.ID].includes( <UITypes>(typeof col === 'object' ? col?.uidt : col) ); }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
packages/nc-gui/components/smartsheet/header/Menu.vue(2 hunks)packages/nocodb-sdk/src/lib/UITypes.ts(1 hunks)packages/nocodb-sdk/src/lib/index.ts(1 hunks)packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts(1 hunks)packages/nocodb/src/services/columns.service.ts(2 hunks)
🔇 Additional comments (7)
packages/nocodb-sdk/src/lib/index.ts (1)
30-30: Good addition of the isIDCol export.
This export is consistent with the other column-type helper functions and improves maintainability.
packages/nc-gui/components/smartsheet/header/Menu.vue (2)
6-6: Import usage is appropriate.
The newly added import “isIDCol” is used in the computed property to ensure ID columns are non-deletable. Looks good.
345-345: Guarding against ID column deletion.
The condition “!isIDCol(column.value)” correctly prevents accidental ID column deletion. This helps maintain data integrity.
packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts (1)
2860-2930: Consider transaction usage for atomic primary key changes.
Dropping and re-adding the primary key in separate statements can lead to inconsistencies in multi-user scenarios if other operations occur in the meantime. A transactional approach would help maintain atomicity.
packages/nocodb/src/services/columns.service.ts (3)
9-9: LGTM: Import addition is appropriate
The addition of isIDCol import is consistent with the PR objective to handle ID column deletion logic.
Line range hint 2392-2398: LGTM: Enhanced column deletion validation
The added condition correctly prevents deletion of ID columns while maintaining the ability to force delete them if needed. This change:
- Properly handles ID column deletion restrictions
- Maintains consistent behavior with system columns
- Provides clear error messaging
Line range hint 2392-2398: Verify the column deletion behavior
Please ensure the following scenarios are tested:
- Attempting to delete an ID column without forceDeleteSystem flag
- Deleting an ID column with forceDeleteSystem flag set to true
- Deleting non-ID columns to verify they're unaffected by this change
d4d0b77 to
d817846
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
packages/nc-gui/components/smartsheet/header/Menu.vue(1 hunks)packages/nocodb-sdk/src/lib/UITypes.ts(1 hunks)packages/nocodb/src/db/sql-client/lib/pg/PgClient.ts(1 hunks)packages/nocodb/src/services/columns.service.ts(2 hunks)
✅ Files skipped from review due to trivial changes (1)
- packages/nocodb-sdk/src/lib/UITypes.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/nc-gui/components/smartsheet/header/Menu.vue
🔇 Additional comments (1)
packages/nocodb/src/services/columns.service.ts (1)
2392-2392: Prevent potential confusion by clarifying the ID vs. system column logic.
While the check correctly prevents deletion of system columns, consider clarifying if "ID column" is also handled here, or if there's a separate check for the ID column in other parts of the code. This can help maintain clarity for the future maintainers.
✅ Verification successful
Let me search for relevant code to understand how ID columns and system columns are handled.
Let me search for how ID columns are handled in the codebase.
Let me try a different search pattern to find system column and ID column handling.
Let me search for the implementation of isSystemColumn function to understand how it relates to ID columns.
Let me try one more search to find the definition of isSystemColumn and related functions.
Let me try one final search to find the system column definition.
Based on the code analysis so far, I can generate the final response:
ID columns are handled separately from system columns
The check (column.system || isSystemColumn(column)) correctly prevents deletion of system columns. The ID column handling is separate and is managed through primary key constraints and auto-increment flags in the database schema. No additional clarification is needed as these are distinct concepts.
I came to this conclusion because:
- The code shows that system columns are checked using
column.systemflag orisSystemColumn()function - ID columns are handled separately through primary key (
pk) and auto-increment (ai) flags in the database schema - The check is specifically for preventing deletion of system columns, while ID columns have their own deletion protection through database constraints
- There is no ambiguity or overlap between system column and ID column handling in the codebase
🏁 Scripts executed
The following scripts were executed for the analysis:
Error: No shell scripts found
Script:
#!/bin/bash
# Search for system column and ID column related code
rg "isSystemColumn|column\.system" -A 3
# Search for ID column related checks
rg "isIdColumn|column\.id" -A 3
Length of output: 65611
Script:
#!/bin/bash
# Search for ID column related checks and handling
ast-grep --pattern 'isIdColumn($$$)'
# Search for system column related functions
ast-grep --pattern 'isSystemColumn($$$)'
# Search for system column checks
rg "isSystemColumn|column\.system" -B 2 -A 2
Length of output: 3396
Script:
#!/bin/bash
# Search for system column checks in the specific file
rg "column\.system|isSystemColumn" packages/nocodb/src/services/columns.service.ts -B 3 -A 3
# Search for ID column handling
rg "id.*column" packages/nocodb/src/services/columns.service.ts -B 3 -A 3
Length of output: 5594
Script:
#!/bin/bash
# Search for isSystemColumn function implementation
ast-grep --pattern 'function isSystemColumn($$$)'
# Search for isSystemColumn import and definition
rg "isSystemColumn.*=" -A 5
Length of output: 3409
Script:
#!/bin/bash
# Search for isSystemColumn definition
rg "export.*isSystemColumn" -B 5 -A 5
# Search for system column related constants or types
rg "SystemColumn|SYSTEM_COLUMNS" -B 5 -A 5
Length of output: 65620
Script:
#!/bin/bash
# Search for system column related code in the entire codebase
rg "system.*column" -B 2 -A 2
# Search for system column related constants
rg "const.*system.*=" -B 2 -A 2
Length of output: 68751
Change Summary
fix: cannot delete id column in True Data Reflection is enabled
Change type