https://github.com/go-gorm/gorm/issues/4760
(t *testing.T)
| 1021 | |
| 1022 | // https://github.com/go-gorm/gorm/issues/4760 |
| 1023 | func TestMigrateAutoIncrement(t *testing.T) { |
| 1024 | type AutoIncrementStruct struct { |
| 1025 | ID int64 `gorm:"primarykey;autoIncrement"` |
| 1026 | Field1 uint32 `gorm:"column:field1"` |
| 1027 | Field2 float32 `gorm:"column:field2"` |
| 1028 | } |
| 1029 | |
| 1030 | if err := DB.AutoMigrate(&AutoIncrementStruct{}); err != nil { |
| 1031 | t.Fatalf("AutoMigrate err: %v", err) |
| 1032 | } |
| 1033 | |
| 1034 | const ROWS = 10 |
| 1035 | for idx := 0; idx < ROWS; idx++ { |
| 1036 | if err := DB.Create(&AutoIncrementStruct{}).Error; err != nil { |
| 1037 | t.Fatalf("create auto_increment_struct fail, err: %v", err) |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | rows := make([]*AutoIncrementStruct, 0, ROWS) |
| 1042 | if err := DB.Order("id ASC").Find(&rows).Error; err != nil { |
| 1043 | t.Fatalf("find auto_increment_struct fail, err: %v", err) |
| 1044 | } |
| 1045 | |
| 1046 | ids := make([]int64, 0, len(rows)) |
| 1047 | for _, row := range rows { |
| 1048 | ids = append(ids, row.ID) |
| 1049 | } |
| 1050 | lastID := ids[len(ids)-1] |
| 1051 | |
| 1052 | if err := DB.Where("id IN (?)", ids).Delete(&AutoIncrementStruct{}).Error; err != nil { |
| 1053 | t.Fatalf("delete auto_increment_struct fail, err: %v", err) |
| 1054 | } |
| 1055 | |
| 1056 | newRow := &AutoIncrementStruct{} |
| 1057 | if err := DB.Create(newRow).Error; err != nil { |
| 1058 | t.Fatalf("create auto_increment_struct fail, err: %v", err) |
| 1059 | } |
| 1060 | |
| 1061 | AssertEqual(t, newRow.ID, lastID+1) |
| 1062 | } |
| 1063 | |
| 1064 | // https://github.com/go-gorm/gorm/issues/5320 |
| 1065 | func TestPrimarykeyID(t *testing.T) { |
nothing calls this directly
no test coverage detected