Skip to content

Commit 692f728

Browse files
authored
Merge pull request #2133 from ly303550688/master
PG临时主键插入或更新时自增主键不插入
2 parents 8d796b7 + ffb7d38 commit 692f728

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

FreeSql.Tests/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLInsertOrUpdateTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,37 @@ ON CONFLICT(""id"") DO UPDATE SET
9898
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
9999
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
100100
}
101+
101102
class tbiou02
102103
{
103104
public int id { get; set; }
104105
public string name { get; set; }
105106
}
107+
108+
[Fact]
109+
public void InsertOrUpdate_TempPrimary()
110+
{
111+
fsql.Delete<tbiou_temp>().Where("1=1").ExecuteAffrows();
112+
var iou = fsql.InsertOrUpdate<tbiou_temp>().SetSource(new tbiou_temp { name = "01", description = "testval" }, m => new { m.name });
113+
var sql = iou.ToSql();
114+
Assert.Equal(@"INSERT INTO ""tbiou_temp""(""name"", ""description"") VALUES('01', 'testval')
115+
ON CONFLICT(""name"") DO UPDATE SET
116+
""description"" = EXCLUDED.""description""", sql);
117+
Assert.Equal(1, iou.ExecuteAffrows());
118+
119+
var iou2 = fsql.InsertOrUpdate<tbiou_temp>().SetSource(new tbiou_temp { name = "01", description = "testval2" }, m => new { m.name }).ExecuteAffrows();
120+
Assert.Equal(1, iou2);
121+
122+
}
123+
[Index("uix_tbiou_temp_name", "name", true)]
124+
class tbiou_temp
125+
{
126+
[Column(IsPrimary = true, IsIdentity = true)]
127+
public int id { get; set; }
128+
129+
public string name { get; set; }
130+
public string description { get; set; }
131+
}
106132
[Fact]
107133
public void InsertOrUpdate_OnePrimaryAndIdentity()
108134
{

Providers/FreeSql.Provider.PostgreSQL/Curd/PostgreSQLInsertOrUpdate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ string getInsertSql(List<T1> data, bool flagInsert, bool noneParameter)
5858
if (IdentityColumn != null && flagInsert) sql = insert.ToSql();
5959
else
6060
{
61-
var ocdu = new OnConflictDoUpdate<T1>(insert.InsertIdentity());
61+
var ocdu = new OnConflictDoUpdate<T1>(_tempPrimarys?.Length > 0 ? insert : insert.InsertIdentity());
6262
ocdu._tempPrimarys = _tempPrimarys;
6363
var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
6464
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false);

0 commit comments

Comments
 (0)