fix(设置): 修复站点设置保存失败的问题

问题:
1. Setting.value 字段设置了 NotEmpty() 约束,导致保存空字符串值时验证失败
2. 数据库 settings 表缺少 key 字段的唯一约束,导致 ON CONFLICT 语句执行失败

修复:
- 移除 ent/schema/setting.go 中 value 字段的 NotEmpty() 约束
- 新增迁移 015_fix_settings_unique_constraint.sql 添加缺失的唯一约束
- 添加3个回归测试确保空值保存功能正常

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
yangjianbo
2025-12-31 15:31:26 +08:00
parent d77d0544d0
commit 6f6dc3032c
7 changed files with 75 additions and 22 deletions

View File

@@ -415,10 +415,6 @@ func init() {
return nil
}
}()
// settingDescValue is the schema descriptor for value field.
settingDescValue := settingFields[1].Descriptor()
// setting.ValueValidator is a validator for the "value" field. It is called by the builders before save.
setting.ValueValidator = settingDescValue.Validators[0].(func(string) error)
// settingDescUpdatedAt is the schema descriptor for updated_at field.
settingDescUpdatedAt := settingFields[2].Descriptor()
// setting.DefaultUpdatedAt holds the default value on creation for the updated_at field.

View File

@@ -36,7 +36,6 @@ func (Setting) Fields() []ent.Field {
NotEmpty().
Unique(),
field.String("value").
NotEmpty().
SchemaType(map[string]string{
dialect.Postgres: "text",
}),

View File

@@ -44,8 +44,6 @@ func ValidColumn(column string) bool {
var (
// KeyValidator is a validator for the "key" field. It is called by the builders before save.
KeyValidator func(string) error
// ValueValidator is a validator for the "value" field. It is called by the builders before save.
ValueValidator func(string) error
// DefaultUpdatedAt holds the default value on creation for the "updated_at" field.
DefaultUpdatedAt func() time.Time
// UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field.

View File

@@ -102,11 +102,6 @@ func (_c *SettingCreate) check() error {
if _, ok := _c.mutation.Value(); !ok {
return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "Setting.value"`)}
}
if v, ok := _c.mutation.Value(); ok {
if err := setting.ValueValidator(v); err != nil {
return &ValidationError{Name: "value", err: fmt.Errorf(`ent: validator failed for field "Setting.value": %w`, err)}
}
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Setting.updated_at"`)}
}

View File

@@ -110,11 +110,6 @@ func (_u *SettingUpdate) check() error {
return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "Setting.key": %w`, err)}
}
}
if v, ok := _u.mutation.Value(); ok {
if err := setting.ValueValidator(v); err != nil {
return &ValidationError{Name: "value", err: fmt.Errorf(`ent: validator failed for field "Setting.value": %w`, err)}
}
}
return nil
}
@@ -254,11 +249,6 @@ func (_u *SettingUpdateOne) check() error {
return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "Setting.key": %w`, err)}
}
}
if v, ok := _u.mutation.Value(); ok {
if err := setting.ValueValidator(v); err != nil {
return &ValidationError{Name: "value", err: fmt.Errorf(`ent: validator failed for field "Setting.value": %w`, err)}
}
}
return nil
}