diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderHelper.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderHelper.cs index 16934dae0..d1bb0b1da 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderHelper.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderHelper.cs @@ -200,7 +200,7 @@ namespace SqlSugar { foreach (var item in insertData) { - this._Context.Insertable(insertData).ExecuteCommandIdentityIntoEntity(); + this._Context.Insertable(item).ExecuteCommandIdentityIntoEntity(); } } else diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/ConnMoreSettings.cs b/Src/Asp.NetCore2/SqlSugar/Entities/ConnMoreSettings.cs index 9fefbb350..3e851e7e1 100644 --- a/Src/Asp.NetCore2/SqlSugar/Entities/ConnMoreSettings.cs +++ b/Src/Asp.NetCore2/SqlSugar/Entities/ConnMoreSettings.cs @@ -1,4 +1,5 @@ -using System; +using SqlSugar.Realization.PostgreSQL; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -39,5 +40,6 @@ namespace SqlSugar public DbType? DatabaseModel { get;set; } public bool ClickHouseEnableFinal { get; set; } public bool EnableJsonb { get; set; } + public PostgresIdentityStrategy PostgresIdentityStrategy { get; set; } = PostgresIdentityStrategy.Serial; // 兼容性处理,默认使用Serial } } diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs index 492dc8e23..ac416eea2 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/DbMaintenance/PostgreSQLDbMaintenance.cs @@ -485,6 +485,7 @@ WHERE tgrelid = '"+tableName+"'::regclass"); { List columnArray = new List(); Check.Exception(columns.IsNullOrEmpty(), "No columns found "); + var identityStrategy = this.Context.CurrentConnectionConfig.MoreSettings?.PostgresIdentityStrategy; foreach (var item in columns) { string columnName = item.DbColumnName; @@ -508,17 +509,26 @@ WHERE tgrelid = '"+tableName+"'::regclass"); string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName.ToLower(isAutoToLowerCodeFirst)), dataType, dataSize, nullType, primaryKey, ""); if (item.IsIdentity) { - if (dataType?.ToLower() == "int") + if(identityStrategy == Realization.PostgreSQL.PostgresIdentityStrategy.Serial) { - dataSize = "int4"; + if (dataType?.ToLower() == "int") + { + dataSize = "int4"; + } + else if (dataType?.ToLower() == "long") + { + dataSize = "int8"; + } + string length = dataType.Substring(dataType.Length - 1); + string identityDataType = "serial" + length; + addItem = addItem.Replace(dataType, identityDataType); } - else if (dataType?.ToLower() == "long") + else if (identityStrategy == Realization.PostgreSQL.PostgresIdentityStrategy.Identity) { - dataSize = "int8"; + string length = dataType.Substring(dataType.Length - 1); + string identityDataType = "INT" + length + " GENERATED BY DEFAULT AS IDENTITY"; + addItem = addItem.Replace(dataType, identityDataType); } - string length = dataType.Substring(dataType.Length - 1); - string identityDataType = "serial" + length; - addItem = addItem.Replace(dataType, identityDataType); } columnArray.Add(addItem); } diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgresIdentityStrategy.cs b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgresIdentityStrategy.cs new file mode 100644 index 000000000..0eceabf3f --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Realization/PostgreSQL/PostgresIdentityStrategy.cs @@ -0,0 +1,14 @@ +namespace SqlSugar.Realization.PostgreSQL +{ + public enum PostgresIdentityStrategy + { + /// + /// NEXTVAL() function + /// + Serial = 1, + /// + /// 自增长,PGSQL10+版本最佳实践 + /// + Identity = 2 + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs index b72942e4d..c7df0921f 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs @@ -900,7 +900,8 @@ namespace SqlSugar EnableILike=it.MoreSettings.EnableILike, ClickHouseEnableFinal=it.MoreSettings.ClickHouseEnableFinal, PgSqlIsAutoToLowerSchema=it.MoreSettings.PgSqlIsAutoToLowerSchema, - EnableJsonb=it.MoreSettings.EnableJsonb + EnableJsonb=it.MoreSettings.EnableJsonb, + PostgresIdentityStrategy = it.MoreSettings.PostgresIdentityStrategy }, SqlMiddle = it.SqlMiddle == null ? null : new SqlMiddle