SqlSugar.SqlSugarException: 42601: unterminated quoted ident 返回

SqlSugar 沟通中
4 571
该叫什么 yiyun 发布于1周前
悬赏:0 飞吻


异常

SqlSugar.SqlSugarException: 42601: unterminated quoted identifier at or near "") VALUES"


POSITION: 55

   at SqlSugar.Check.Exception(Boolean isException, String message, String[] args)

   at SqlSugar.CodeFirstProvider.InitTables(Type entityType)

   at SqlSugar.SplitCodeFirstProvider._InitTables(Type type)

   at SqlSugar.SplitCodeFirstProvider.InitTables(Type type)

   at SqlSugar.SplitCodeFirstProvider.InitTables[T]()


代码


private readonly ISqlSugarClient _db;

public static SqlSugarScopeProvider His(this ISqlSugarClient db)

{

    SqlSugarScopeProvider rtn = db.AsTenant().GetConnectionScope("His");


    return rtn;

}


// 这里报错

_db.His().CodeFirst

    .SplitTables() //标识分表

    .InitTables<BUserEntity>(); //程序启动时加这一行,如果一张表没有会初始化一张




实体类

[SplitTable(SplitType.Month)]//按月分表 (自带分表支持 年、季、月、周、日)

[SugarTable("BUser_{year}{month}{day}")]//3个变量必须要有,这么设计为了兼容开始按年,后面改成按月、按日

[SugarIndex("index_{split_table}_name", nameof(BUserEntity.CreatedTime), OrderByType.Desc)]

public class BUserEntity : BaseEntityWithPkIdWithHashId<BUserEntity>

{

    /// <summary>

    /// 分表字段 在插入的时候会根据这个字段插入哪个表,在更新删除的时候用这个字段找出相关表

    /// </summary>

    [SplitField]

    [SugarColumn(IsNullable = false, ColumnDataType = "timestamptz")]

    public new DateTimeOffset CreatedTime { get; set; }


    [Required]

    [SugarColumn(IsNullable = false)]

    public long UID { get; set; }


    [SugarColumn(IsNullable = true)]

    public string Name { get; set; }


    [SugarColumn(IsNullable = true)]

    public string Sex { get; set; }


    /// <summary>

    /// 头像url

    /// </summary>

    [SugarColumn(IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]

    public string FaceUrl { get; set; }


    /// <summary>

    /// 个人前面

    /// </summary>

    [SugarColumn(IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]

    public string Sign { get; set; }


    [SugarColumn(IsNullable = true)]

    public int? Level { get; set; }


    /// <summary>

    /// 黄v-个人认证

    /// </summary>

    [SugarColumn(IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]

    public string OfficialTitle { get; set; }


    /// <summary>

    /// 黄v-个人认证

    /// </summary>

    [SugarColumn(IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]

    public string OfficialDesc { get; set; }


    [SugarColumn(IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]

    public string LiveRoomUrl { get; set; }


    [SugarColumn(IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]

    public string LiveRoomTitle { get; set; }


    [SugarColumn(IsNullable = true)]

    public long? LiveRoomId { get; set; }


    [SugarColumn(IsNullable = true)]

    public string Birthday { get; set; }


    [SugarColumn(IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]

    public string School { get; set; }

    

    /// <summary>

    /// 用户稿件数

    /// </summary>

    [SugarColumn(IsNullable = true)]

    public long? ArchiveCount { get; set; }

    

    /// <summary>

    /// 粉丝数

    /// </summary>

    [SugarColumn(IsNullable = true)]

    public long? FansCount { get; set; }


    /// <summary>

    /// 关注数

    /// </summary>

    [SugarColumn(IsNullable = true)]

    public long? FollowingCount { get; set; }


    /// <summary>

    /// 用户状态

    /// </summary>

    /// <remarks>

    /// 0:正常

    /// -2:被封禁

    /// </remarks>

    [SugarColumn(IsNullable = true)]

    public long? UserStatus { get; set; }

}

public abstract class BaseEntityWithPkId : BaseEntity

{

    [Key]

    [SugarColumn(IsPrimaryKey = true, IsIdentity = false)]

    public string Id { get; set; }

}


public abstract class BaseEntityWithPkIdWithHashId<T> : BaseEntityWithPkId

    where T : BaseEntityWithPkIdWithHashId<T>

{

    [Required]

    [SugarColumn(IsNullable = false)]

    public string HashId { get; set; }


    #region CalculateHashId

    public virtual string CalculateHashId()

    {

        var newEntity = this.Adapt<T>();


        // 需要设置为默认值的属性名列表

        var defaultPropertyNames = new[]

        {

            nameof(Id),

            nameof(HashId),

            nameof(CreatedTime),

            nameof(CreatedTimeStamp),

            nameof(UpdatedTime),

            nameof(UpdatedTimeStamp),

            nameof(DeletedTime),

            nameof(DeletedTimeStamp)

        };


        // fixed: 使用反射避免需要 new 覆盖隐藏基类时的属性赋值 default 到基类

        // 使用反射获取所有属性(包括继承的和重写的)

        var properties = newEntity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);


        // 只设置指定属性为默认值

        foreach (var property in properties)

        {

            if (property.CanWrite && defaultPropertyNames.Contains(property.Name))

            {

                property.SetValue(newEntity, GetDefaultValue(property.PropertyType));

            }

        }


        string hashId = JsonHashCalculatorUtil.CalculateJsonHash(newEntity);

        return hashId;

    }


    /// <summary>

    /// 获取类型的默认值

    /// </summary>

    private static object GetDefaultValue(Type type)

    {

        if (type.IsValueType)

        {

            return Activator.CreateInstance(type);

        }

        return null;

    }


    public string CalculateHashId(params string[] pars)

    {

        string hashId = EntityHashCalculatorUtil.CalculateHash(pars);


        return hashId;

    }

    #endregion

}



使用

<PackageReference Include="SqlSugarCore" Version="5.1.4.167-preview09" />

数据库

new ConnectionConfig()

{

    ConfigId = "His",

    DbType = SqlSugar.DbType.PostgreSQL,

    ConnectionString = settings.DbConn.His.ConnStr,

    IsAutoCloseConnection = true,

    // https://www.donet5.com/Home/Doc?typeId=1221

    MoreSettings = new ConnMoreSettings()

    {

        PgSqlIsAutoToLower = false,//增删查改支持驼峰表

        PgSqlIsAutoToLowerCodeFirst = false, // 建表建驼峰表。5.1.3.30 

    }

},


热忱回答4

  • yiyun yiyun VIP0
    1周前

    补充
    发现之前的这张分表不知道什么时候 HashId 还有了个默认值,明明没设置

    image.png

    0 回复
  • yiyun yiyun VIP0
    1周前

    @yiyun:这张分表是 text 类型,是因为之前没分表,用 EF Core CodeFirst 生成的才对应的 text 类型,后面手动改表名称加日期

    0 回复
  • yiyun yiyun VIP0
    1周前

    @yiyun:解决:发现把 BUser_20250601 里的 HashId 的默认值置空,就前面截图那个不知道什么时候有的,然后再次运行就成功了

    0 回复
  • yiyun yiyun VIP0
    1周前

    @yiyun:对了,之前这张表还默认奇妙生成了一个 HashId ) values 啥的字段,当时早就切到使用 SqlSugar 分表了,然后我没在意就把这个字段删除了

    0 回复