From 17cb502564346f7af1de5618a0e57d6685d2e72c Mon Sep 17 00:00:00 2001 From: yayaxxww Date: Fri, 24 Oct 2025 20:28:21 +0800 Subject: [PATCH] Fix PostgreSQL bulk copy identity column handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Modified FastestProvider to handle identity columns differently for PostgreSQL/Vastbase - Added logic to exclude identity columns from bulk copy operations for PostgreSQL - Commented out identity column restriction in PostgreSQLFastBuilder - PostgreSQL bulk copy now supports tables with identity columns by excluding them 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../Abstract/FastestProvider/Private.cs | 23 +++++++++++++++++-- .../SqlBuilder/PostgreSQLFastBuilder.cs | 10 ++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/Private.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/Private.cs index 6858e62e5..bda83a193 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/Private.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/Private.cs @@ -86,7 +86,7 @@ namespace SqlSugar List uInt64TypeName = new List(); foreach (DataColumn item in tempDataTable.Columns) { - if (item.DataType == typeof(UInt64)) + if (item.DataType == typeof(UInt64)) { uInt64TypeName.Add(item.ColumnName); } @@ -99,8 +99,27 @@ namespace SqlSugar dt.Columns.Add(item.ColumnName, item.DataType); } } + + bool supportIdentity = true; + if (this.context.CurrentConnectionConfig.DbType == DbType.PostgreSQL || this.context.CurrentConnectionConfig.DbType == DbType.Vastbase) + { + supportIdentity = false; + } + + if (!supportIdentity) + { + // PostgreSQL/Vastbase不支持自增主键导入 + foreach (var identityColumnInfo in this.entityInfo.Columns.Where(it => it.IsIdentity)) + { + if (dt.Columns.Contains(identityColumnInfo.DbColumnName)) + { + dt.Columns.Remove(identityColumnInfo.DbColumnName); + } + } + } + dt.TableName = GetTableName(); - var columns = entityInfo.Columns; + var columns = supportIdentity ? entityInfo.Columns : entityInfo.Columns.Where(it => !it.IsIdentity).ToList(); if (columns.Where(it=>!it.IsIgnore).Count() > tempDataTable.Columns.Count) { var tempColumns = tempDataTable.Columns.Cast().Select(it=>it.ColumnName); diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLFastBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLFastBuilder.cs index 8f7871cc6..19b9bce38 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLFastBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLFastBuilder.cs @@ -47,11 +47,11 @@ namespace SqlSugar var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(this.entityInfo.DbTableName); try { - var identityColumnInfo = this.entityInfo.Columns.FirstOrDefault(it => it.IsIdentity); - if (identityColumnInfo != null) - { - throw new Exception("PgSql bulkcopy no support identity"); - } + //var identityColumnInfo = this.entityInfo.Columns.FirstOrDefault(it => it.IsIdentity); + //if (identityColumnInfo != null) + //{ + // throw new Exception("PgSql bulkcopy no support identity"); + //} BulkCopy(dt, copyString, conn, columns); } catch (Exception ex)