From ebbe5b71b6ef1f4dbf70d07e2bfc0c54a2219fc0 Mon Sep 17 00:00:00 2001 From: lijunshi Date: Sat, 20 Apr 2024 22:10:07 +0800 Subject: [PATCH] fix: #2444 --- dialects/mysql.go | 3 +++ tests/schema_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/dialects/mysql.go b/dialects/mysql.go index d11c728b..5a7751b8 100644 --- a/dialects/mysql.go +++ b/dialects/mysql.go @@ -402,6 +402,9 @@ func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string { // ModifyColumnSQL returns a SQL to modify SQL func (db *mysql) ModifyColumnSQL(tableName string, col *schemas.Column) string { s, _ := ColumnString(db.dialect, col, false, true) + if col.IsAutoIncrement { + s += " " + db.AutoIncrStr() + } if col.Comment != "" { s += fmt.Sprintf(" COMMENT '%s'", col.Comment) } diff --git a/tests/schema_test.go b/tests/schema_test.go index 3657950e..148102a3 100644 --- a/tests/schema_test.go +++ b/tests/schema_test.go @@ -205,6 +205,7 @@ func TestSyncTable(t *testing.T) { tableInfo, err = testEngine.TableInfo(new(SyncTable3)) assert.NoError(t, err) assert.EqualValues(t, testEngine.Dialect().SQLType(tables[0].GetColumn("name")), testEngine.Dialect().SQLType(tableInfo.GetColumn("name"))) + } func TestSyncTable2(t *testing.T) { @@ -396,6 +397,7 @@ func TestIndexAndUnique(t *testing.T) { assert.NoError(t, testEngine.DropIndexes(&IndexOrUnique{})) } + func TestMetaInfo(t *testing.T) { assert.NoError(t, PrepareEngine()) assert.NoError(t, testEngine.Sync(new(CustomTableName), new(IndexOrUnique))) @@ -537,8 +539,11 @@ func TestModifyColum(t *testing.T) { }) _, err := testEngine.Exec(alterSQL) assert.NoError(t, err) + + } + type TestCollateColumn struct { Id int64 UserId int64 `xorm:"unique(s)"` @@ -751,3 +756,34 @@ func getKeysFromMap(m map[string]*schemas.Index) []string { } return ss } + + +func TestSync2_3(t *testing.T) { + assert.NoError(t, PrepareEngine()) + { + type User struct { + Id int `xorm:"pk autoincr 'id'"` + Name string `xorm:"'name' notnull comment('nickname')" json:"name"` + } + + assert.NoError(t, testEngine.Sync2(new(User))) + } + + // add comment for id column + type User struct { + Id int `xorm:"pk autoincr 'id' comment('primary key')"` + Name string `xorm:"'name' notnull comment('nickname')" json:"name"` + } + + assert.NoError(t, testEngine.Sync2(new(User))) + tables, err := testEngine.DBMetas() + assert.NoError(t, err) + tableInfo, err := testEngine.TableInfo(new(User)) + + assert.EqualValues(t, tables[0].GetColumn("id").IsAutoIncrement, tableInfo.GetColumn("id").IsAutoIncrement) + assert.EqualValues(t, tables[0].GetColumn("id").Name, tableInfo.GetColumn("id").Name) + assert.EqualValues(t, tables[0].GetColumn("id").SQLType, tableInfo.GetColumn("id").SQLType) + assert.EqualValues(t, tables[0].GetColumn("id").Nullable, tableInfo.GetColumn("id").Nullable) + assert.EqualValues(t, tables[0].GetColumn("id").Comment, tableInfo.GetColumn("id").Comment) + assert.EqualValues(t, tables[0].GetColumn("id").IsPrimaryKey, tableInfo.GetColumn("id").IsPrimaryKey) +} \ No newline at end of file