Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the AUTO_RANDOM Field #5981

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ColumnType interface {
ColumnType() (columnType string, ok bool) // varchar(64)
PrimaryKey() (isPrimaryKey bool, ok bool)
AutoIncrement() (isAutoIncrement bool, ok bool)
AutoRandom() (isAutoRandom bool, ok bool)
Length() (length int64, ok bool)
DecimalSize() (precision int64, scale int64, ok bool)
Nullable() (nullable bool, ok bool)
Expand Down
6 changes: 6 additions & 0 deletions migrator/column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ColumnType struct {
PrimaryKeyValue sql.NullBool
UniqueValue sql.NullBool
AutoIncrementValue sql.NullBool
AutoRandomValue sql.NullBool
LengthValue sql.NullInt64
DecimalSizeValue sql.NullInt64
ScaleValue sql.NullInt64
Expand Down Expand Up @@ -59,6 +60,11 @@ func (ct ColumnType) AutoIncrement() (isAutoIncrement bool, ok bool) {
return ct.AutoIncrementValue.Bool, ct.AutoIncrementValue.Valid
}

// AutoRandom returns the column is auto random or not.
func (ct ColumnType) AutoRandom() (isAutoRandom bool, ok bool) {
return ct.AutoRandomValue.Bool, ct.AutoRandomValue.Valid
}

// Length returns the column type length for variable length column types
func (ct ColumnType) Length() (length int64, ok bool) {
if ct.LengthValue.Valid {
Expand Down
7 changes: 4 additions & 3 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import "time"

// Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt
// It may be embedded into your model or you may build your own model without it
// type User struct {
// gorm.Model
// }
//
// type User struct {
// gorm.Model
// }
type Model struct {
ID uint `gorm:"primarykey"`
CreatedAt time.Time
Expand Down
10 changes: 7 additions & 3 deletions schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Field struct {
PrimaryKey bool
AutoIncrement bool
AutoIncrementIncrement int64
AutoRandom bool
Creatable bool
Updatable bool
Readable bool
Expand Down Expand Up @@ -111,7 +112,8 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
Readable: true,
PrimaryKey: utils.CheckTruth(tagSetting["PRIMARYKEY"], tagSetting["PRIMARY_KEY"]),
AutoIncrement: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"]),
AutoRandom: utils.CheckTruth(tagSetting["AUTORANDOM"]),
HasDefaultValue: utils.CheckTruth(tagSetting["AUTOINCREMENT"], tagSetting["AUTORANDOM"]),
NotNull: utils.CheckTruth(tagSetting["NOT NULL"], tagSetting["NOTNULL"]),
Unique: utils.CheckTruth(tagSetting["UNIQUE"]),
Comment: tagSetting["COMMENT"],
Expand Down Expand Up @@ -406,11 +408,13 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
if !utils.CheckTruth(ef.TagSettings["PRIMARYKEY"], ef.TagSettings["PRIMARY_KEY"]) {
ef.PrimaryKey = false

if val, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(val) {
if autoIncrVal, ok := ef.TagSettings["AUTOINCREMENT"]; !ok || !utils.CheckTruth(autoIncrVal) {
ef.AutoIncrement = false
} else if autoRandVal, ok := ef.TagSettings["AUTORANDOM"]; !ok || !utils.CheckTruth(autoRandVal) {
ef.AutoRandom = false
}

if !ef.AutoIncrement && ef.DefaultValue == "" {
if !ef.AutoIncrement && !ef.AutoRandom && ef.DefaultValue == "" {
ef.HasDefaultValue = false
}
}
Expand Down
23 changes: 12 additions & 11 deletions schema/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,17 @@ func (schema *Schema) parseRelation(field *Field) *Relationship {
}

// User has many Toys, its `Polymorphic` is `Owner`, Pet has one Toy, its `Polymorphic` is `Owner`
// type User struct {
// Toys []Toy `gorm:"polymorphic:Owner;"`
// }
// type Pet struct {
// Toy Toy `gorm:"polymorphic:Owner;"`
// }
// type Toy struct {
// OwnerID int
// OwnerType string
// }
//
// type User struct {
// Toys []Toy `gorm:"polymorphic:Owner;"`
// }
// type Pet struct {
// Toy Toy `gorm:"polymorphic:Owner;"`
// }
// type Toy struct {
// OwnerID int
// OwnerType string
// }
func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Field, polymorphic string) {
relation.Polymorphic = &Polymorphic{
Value: schema.Table,
Expand Down Expand Up @@ -641,7 +642,7 @@ func (rel *Relationship) ToQueryConditions(ctx context.Context, reflectValue ref
}

func copyableDataType(str DataType) bool {
for _, s := range []string{"auto_increment", "primary key"} {
for _, s := range []string{"auto_increment", "auto_random", "primary key"} {
if strings.Contains(strings.ToLower(string(str)), s) {
return false
}
Expand Down
13 changes: 8 additions & 5 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,15 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
switch field.GORMDataType {
case Int, Uint:
if _, ok := field.TagSettings["AUTOINCREMENT"]; !ok {
if !field.HasDefaultValue || field.DefaultValueInterface != nil {
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)
}
// also not have AUTORANDOM tag
if _, ok := field.TagSettings["AUTORANDOM"]; !ok {
if !field.HasDefaultValue || field.DefaultValueInterface != nil {
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)
}

field.HasDefaultValue = true
field.AutoIncrement = true
field.HasDefaultValue = true
field.AutoIncrement = true
}
}
case String:
if _, ok := field.TagSettings["PRIMARYKEY"]; !ok {
Expand Down
8 changes: 5 additions & 3 deletions tests/connpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ func (c *wrapperConnPool) Ping() error {
}

// If you use BeginTx returned *sql.Tx as shown below then you can't record queries in a transaction.
// func (c *wrapperConnPool) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
// return c.db.BeginTx(ctx, opts)
// }
//
// func (c *wrapperConnPool) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
// return c.db.BeginTx(ctx, opts)
// }
//
// You should use BeginTx returned gorm.Tx which could wrap *sql.Tx then you can record all queries.
func (c *wrapperConnPool) BeginTx(ctx context.Context, opts *sql.TxOptions) (gorm.ConnPool, error) {
tx, err := c.db.BeginTx(ctx, opts)
Expand Down
1 change: 1 addition & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/lib/pq v1.10.7
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/microsoft/go-mssqldb v0.19.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
gorm.io/driver/mysql v1.4.5
gorm.io/driver/postgres v1.4.6
gorm.io/driver/sqlite v1.4.4
Expand Down