Update Pgsql GetColumnInfosByTableName

This commit is contained in:
sunkaixuna 2021-12-20 13:36:27 +08:00
parent b43b6181a5
commit be84b3a157
2 changed files with 45 additions and 0 deletions

View File

@ -30,7 +30,24 @@ namespace OrmTest
Db.Insertable(new List<UnitDateOfTime22> { new UnitDateOfTime22() { DateTimeOffset1 = DateTimeOffset.Now }, new UnitDateOfTime22() { DateTimeOffset1 = DateTimeOffset.Now } }).ExecuteCommand();
var list2 = Db.Queryable<UnitDateOfTime22>().ToList();
Db.CodeFirst.InitTables<UnitTestPk>();
Db.CodeFirst.InitTables<UnitTestPk>();
Db.CodeFirst.InitTables<UnitTestPk>();
}
public class UnitTestPk
{
[SqlSugar.SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
public int id { get; set; }
[SqlSugar.SugarColumn(IsPrimaryKey = true )]
public long id2 { get; set; }
[SqlSugar.SugarColumn(IsPrimaryKey = true )]
public Guid id3 { get; set; }
public string Name { get; set; }
}
[SqlSugar.SugarTable("UnitDateOfTime221")]
public class UnitDateOfTime22
{

View File

@ -394,6 +394,34 @@ namespace SqlSugar
if (result == null || result.Count() == 0)
{
result = base.GetColumnInfosByTableName(tableName, isCache);
}
try
{
string sql = $@"select
kcu.column_name as key_column
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on kcu.constraint_name = tco.constraint_name
and kcu.constraint_schema = tco.constraint_schema
and kcu.constraint_name = tco.constraint_name
where tco.constraint_type = 'PRIMARY KEY'
and kcu.table_schema='public' and
upper(kcu.table_name)=upper('{tableName}')";
var pkList = this.Context.Ado.SqlQuery<string>(sql);
if (pkList.Count >1)
{
foreach (var item in result)
{
if (pkList.Select(it=>it.ToUpper()).Contains(item.DbColumnName.ToUpper()))
{
item.IsPrimarykey = true;
}
}
}
}
catch
{
}
return result;
}