fix(core): include auto-increment primary keys in copied INSERTs

This commit is contained in:
dienaso authored and GitHub committed 2026-07-30 21:10:19 +08:00
1 parent a7a0b69605
commit 31ad4cfaca
4 files changed
+60 -5

No files matched your search

@@ -417,6 +417,22 @@ describe("useDataGridExport prepared row statements", () => {
expect(state.canCopyWithExtractor("sql-inserts")).toBe(false);
});
it("keeps an auto-increment primary key when copying only the primary key column as INSERT", () => {
const autoIncrementTable: DataGridTableMeta = {
tableName: "users",
primaryKeys: ["id"],
columns: [
{ name: "id", data_type: "int", is_nullable: false, is_primary_key: true, extra: "auto_increment" },
{ name: "name", data_type: "varchar", is_nullable: false },
],
};
const matrix: CellSelectionMatrix = { rowIndexes: [0], columnIndexes: [0], columns: ["id"], rows: [[1]] };
const state = createExportState(autoIncrementTable, ["id", "name"], matrix);
expect(state.canCopyWithExtractor("sql-inserts")).toBe(true);
});
it("sends only selected values for non-SQL extraction and marks column selections", async () => {
const matrix: CellSelectionMatrix = { rowIndexes: [0], columnIndexes: [1], columns: ["name"], rows: [["Ada"]] };
vi.mocked(extractDataGridSelection).mockResolvedValueOnce({
@@ -1386,7 +1386,8 @@ function isCopyInsertOmittedColumn(databaseType: DatabaseType | undefined, colum
const extra = columnInfo?.extra?.toLowerCase() ?? "";
const isAutoGenerated = /\b(auto_increment|autoincrement|identity)\b/.test(extra);
const isComputed = extra.includes("generated always as") && !extra.includes("identity");
return ((extractorOptions?.sql.skipGeneratedColumns ?? true) && isAutoGenerated) || ((extractorOptions?.sql.skipComputedColumns ?? true) && isComputed);
const isPrimaryKey = (tableMeta?.primaryKeys ?? []).some((key) => normalizeColumnName(key) === normalizeColumnName(column));
return ((extractorOptions?.sql.skipGeneratedColumns ?? true) && isAutoGenerated && !isPrimaryKey) || ((extractorOptions?.sql.skipComputedColumns ?? true) && isComputed);
}
function normalizeColumnName(name: string): string {