xorm: deleted标签中的notnull被忽略 #2013

Closed
opened 2 months ago by laoqiu · 0 comments
laoqiu commented 2 months ago

v1.1.0 升级到 v1.1.2之后

如果deleted中也存在notnull时,notnull无效

func Test_DeletedAt(t *testing.T) {

	type InsertDeletedStruct struct {
		ID        uint64 `xorm:"'ID' pk autoincr"`
		DeletedAt int64  `xorm:"'DELETED_AT' deleted notnull"`
	}

	db, err := xorm.NewEngine("mysql", "root:123456@tcp(127.0.0.1:3306)/test")
	if err != nil {
		t.Error(err)
		return
	}
	db.ShowSQL()

	// assert.NoError(t, db.Sync2(new(InsertDeletedStruct)))

	item := &InsertDeletedStruct{
		ID: 5,
	}
	if _, err := db.InsertOne(item); err != nil {
		assert.NoError(t, err)
	}

}

原因是在tags/parse.go中强制给deletedColumn设置nullable为true,这个是什么操作?

	deletedColumn := table.DeletedColumn()
	// check columns
	if deletedColumn != nil {
		deletedColumn.Nullable = true
	}

直接导致在genInsertColumns函数中,对col.IsDeleted代码段无意义,永远赋为nil,如果设置过notnull还会报setnull的错误.

		if col.IsDeleted {
			colNames = append(colNames, col.Name)
			if !col.Nullable {
				if col.SQLType.IsNumeric() {
					args = append(args, 0)
				} else {
					args = append(args, time.Time{}.Format("2006-01-02 15:04:05"))
				}
			} else {
				args = append(args, nil)
			}
			continue
		}
v1.1.0 升级到 v1.1.2之后 如果deleted中也存在notnull时,notnull无效 ``` func Test_DeletedAt(t *testing.T) { type InsertDeletedStruct struct { ID uint64 `xorm:"'ID' pk autoincr"` DeletedAt int64 `xorm:"'DELETED_AT' deleted notnull"` } db, err := xorm.NewEngine("mysql", "root:123456@tcp(127.0.0.1:3306)/test") if err != nil { t.Error(err) return } db.ShowSQL() // assert.NoError(t, db.Sync2(new(InsertDeletedStruct))) item := &InsertDeletedStruct{ ID: 5, } if _, err := db.InsertOne(item); err != nil { assert.NoError(t, err) } } ``` 原因是在tags/parse.go中强制给deletedColumn设置nullable为true,这个是什么操作? ``` deletedColumn := table.DeletedColumn() // check columns if deletedColumn != nil { deletedColumn.Nullable = true } ``` 直接导致在genInsertColumns函数中,对col.IsDeleted代码段无意义,永远赋为nil,如果设置过notnull还会报setnull的错误. ``` if col.IsDeleted { colNames = append(colNames, col.Name) if !col.Nullable { if col.SQLType.IsNumeric() { args = append(args, 0) } else { args = append(args, time.Time{}.Format("2006-01-02 15:04:05")) } } else { args = append(args, nil) } continue } ```
lunny closed this issue 2 months ago
lunny referenced this issue from a commit 2 months ago
lunny added this to the 1.2.0 milestone 2 months ago
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.