diff --git a/FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs b/FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs index 5b21741d..9bc76f41 100644 --- a/FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs +++ b/FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs @@ -23,5 +23,126 @@ namespace FreeSql.Tests.ClickHouse { fsql.CodeFirst.SyncStructure(typeof(CollectDataEntityUpdate01)); } + + [Fact] + public void Issuse1587Test() + { + fsql.CodeFirst.SyncStructure(typeof(PositionInfoModel)); + } + [Fact] + public void Issuse1587TestOnePrimary() + { + fsql.CodeFirst.SyncStructure(typeof(PositionInfoModel2)); + } + } + + [Table(Name = "table_1")] + [Index("stcd_index", "STCD", false)] + [Index("tm_index", "TM", false)] + [Index("type_index", "TYPE", false)] + public class PositionInfoModel + { + [Column(IsPrimary = true)] + public string STCD + { + set; get; + } + + [Column(IsPrimary = true)] + public DateTime TM + { + set; get; + } + + [Column(IsNullable = false)] + public decimal LON + { + set; get; + } + + [Column(IsNullable = false)] + public decimal LAT + { + set; get; + } + + [Column(IsNullable = false)] + public int TYPE + { + set; get; + } + + [Column(IsNullable = true)] + public decimal SPD + { + set; get; + } + + [Column(IsNullable = true)] + public decimal COG + { + set; get; + } + + [Column(IsNullable = true)] + public DateTime? UT + { + set; get; + } + } + + + [Table(Name = "table_2")] + [Index("stcd_index", "STCD", false)] + [Index("tm_index", "TM", false)] + [Index("type_index", "TYPE", false)] + public class PositionInfoModel2 + { + public string STCD + { + set; get; + } + + [Column(IsPrimary = true)] + public DateTime TM + { + set; get; + } + + [Column(IsNullable = false)] + public decimal LON + { + set; get; + } + + [Column(IsNullable = false)] + public decimal LAT + { + set; get; + } + + [Column(IsNullable = false)] + public int TYPE + { + set; get; + } + + [Column(IsNullable = true)] + public decimal SPD + { + set; get; + } + + [Column(IsNullable = true)] + public decimal COG + { + set; get; + } + + [Column(IsNullable = true)] + public DateTime? UT + { + set; get; + } } } \ No newline at end of file diff --git a/Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs b/Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs index b71c024d..d0c41132 100644 --- a/Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs +++ b/Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs @@ -199,7 +199,7 @@ namespace FreeSql.ClickHouse sb.Remove(sb.Length - 2, 2); sb.Append(" )"); sb.Append(" \r\nPRIMARY KEY "); - sb.Append(ls); + sb.Append($"({ls}) "); sb.Remove(sb.Length - 2, 2).Append(","); } @@ -415,7 +415,7 @@ where a.database in ({0}) and a.table in ({1})", tboldname ?? tbname); sb.Remove(sb.Length - 2, 2); sb.Append(" )"); sb.Append(" \r\nPRIMARY KEY "); - sb.Append(ls); + sb.Append($"({ls}) "); sb.Remove(sb.Length - 2, 2).Append(","); } @@ -455,7 +455,8 @@ where a.database in ({0}) and a.table in ({1})", tboldname ?? tbname); .Append(_commonUtils.QuoteSqlName(tbname[0], tbname[1])).Append(";\r\n"); } - return sb.Length == 0 ? null : sb.ToString(); + var res = sb.Length == 0 ? null : sb.ToString(); + return res; } finally { @@ -514,9 +515,14 @@ where a.database in ({0}) and a.table in ({1})", tboldname ?? tbname); } string CkNullableAdapter(string dbType, bool isPrimary) { - return isPrimary - ? dbType.Replace("Nullable(", "").Replace(")","").Replace(" NOT NULL", "") - : dbType.Replace(" NOT NULL", ""); + return isPrimary switch + { + true when dbType.Contains("Nullable") => dbType.Replace("Nullable(", "") + .Replace(")", "") + .Replace(" NOT NULL", ""), + true => dbType, + _ => dbType.Replace(" NOT NULL", "") + }; }