Merge pull request #2133 from ly303550688/master

PG临时主键插入或更新时自增主键不插入
This commit is contained in:
2881099
2025-10-31 08:48:16 +08:00
committed by GitHub
2 changed files with 27 additions and 1 deletions

View File

@@ -98,11 +98,37 @@ ON CONFLICT(""id"") DO UPDATE SET
var lst = fsql.Select<tbiou02>().Where(a => new[] { 1, 2, 3, 4 }.Contains(a.id)).ToList();
Assert.Equal(4, lst.Where(a => a.name == "00" + a.id).Count());
}
class tbiou02
{
public int id { get; set; }
public string name { get; set; }
}
[Fact]
public void InsertOrUpdate_TempPrimary()
{
fsql.Delete<tbiou_temp>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou_temp>().SetSource(new tbiou_temp { name = "01", description = "testval" }, m => new { m.name });
var sql = iou.ToSql();
Assert.Equal(@"INSERT INTO ""tbiou_temp""(""name"", ""description"") VALUES('01', 'testval')
ON CONFLICT(""name"") DO UPDATE SET
""description"" = EXCLUDED.""description""", sql);
Assert.Equal(1, iou.ExecuteAffrows());
var iou2 = fsql.InsertOrUpdate<tbiou_temp>().SetSource(new tbiou_temp { name = "01", description = "testval2" }, m => new { m.name }).ExecuteAffrows();
Assert.Equal(1, iou2);
}
[Index("uix_tbiou_temp_name", "name", true)]
class tbiou_temp
{
[Column(IsPrimary = true, IsIdentity = true)]
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
}
[Fact]
public void InsertOrUpdate_OnePrimaryAndIdentity()
{

View File

@@ -58,7 +58,7 @@ namespace FreeSql.PostgreSQL.Curd
if (IdentityColumn != null && flagInsert) sql = insert.ToSql();
else
{
var ocdu = new OnConflictDoUpdate<T1>(insert.InsertIdentity());
var ocdu = new OnConflictDoUpdate<T1>(_tempPrimarys?.Length > 0 ? insert : insert.InsertIdentity());
ocdu._tempPrimarys = _tempPrimarys;
var cols = _table.Columns.Values.Where(a => _updateSetDict.ContainsKey(a.Attribute.Name) ||
_tempPrimarys.Contains(a) == false && a.Attribute.CanUpdate == true && a.Attribute.IsIdentity == false && _updateIgnore.ContainsKey(a.Attribute.Name) == false);