dialect/sql/schema: setrange on custom column name of pks (#333)

This commit is contained in:
Ariel Mashraki
2020-02-09 09:41:26 +02:00
committed by GitHub
parent 48d33fde9d
commit 26440c2bc9
12 changed files with 60 additions and 26 deletions

View File

@@ -34,7 +34,7 @@ var (
}
// UsersColumns holds the columns for the "users" table.
UsersColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "oid", Type: field.TypeInt, Increment: true},
{Name: "age", Type: field.TypeInt32},
{Name: "name", Type: field.TypeString, Size: 10},
{Name: "nickname", Type: field.TypeString, Unique: true},

View File

@@ -19,6 +19,8 @@ type User struct {
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id").
StorageKey("oid"),
field.Int32("age"),
field.String("name").
MaxLen(10),

View File

@@ -16,7 +16,7 @@ const (
// Label holds the string label denoting the user type in the database.
Label = "user"
// FieldID holds the string denoting the id field in the database.
FieldID = "id"
FieldID = "oid"
// FieldAge holds the string denoting the age vertex property in the database.
FieldAge = "age"
// FieldName holds the string denoting the name vertex property in the database.
@@ -77,7 +77,7 @@ var (
fields = schema.User{}.Fields()
// descName is the schema descriptor for name field.
descName = fields[1].Descriptor()
descName = fields[2].Descriptor()
// NameValidator is a validator for the "name" field. It is called by the builders before save.
NameValidator = descName.Validators[0].(func(string) error)
)

View File

@@ -20,6 +20,7 @@ import (
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
id *int
age *int32
name *string
nickname *string
@@ -99,6 +100,12 @@ func (uc *UserCreate) SetNillableState(u *user.State) *UserCreate {
return uc
}
// SetID sets the id field.
func (uc *UserCreate) SetID(i int) *UserCreate {
uc.id = &i
return uc
}
// SetParentID sets the parent edge to User by id.
func (uc *UserCreate) SetParentID(id int) *UserCreate {
if uc.parent == nil {
@@ -236,6 +243,10 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
},
}
)
if value := uc.id; value != nil {
u.ID = *value
_spec.ID.Value = *value
}
if value := uc.age; value != nil {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt32,
@@ -374,7 +385,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
}
return nil, err
}
id := _spec.ID.Value.(int64)
u.ID = int(id)
if u.ID == 0 {
id := _spec.ID.Value.(int64)
u.ID = int(id)
}
return u, nil
}

View File

@@ -58,7 +58,7 @@ var (
}
// UsersColumns holds the columns for the "users" table.
UsersColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "oid", Type: field.TypeInt, Increment: true},
{Name: "age", Type: field.TypeInt},
{Name: "name", Type: field.TypeString, Size: 2147483647},
{Name: "nickname", Type: field.TypeString},

View File

@@ -19,6 +19,8 @@ type User struct {
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id").
StorageKey("oid"),
// changing the type of the field.
field.Int("age"),
// extending name field to longtext.

View File

@@ -16,7 +16,7 @@ const (
// Label holds the string label denoting the user type in the database.
Label = "user"
// FieldID holds the string denoting the id field in the database.
FieldID = "id"
FieldID = "oid"
// FieldAge holds the string denoting the age vertex property in the database.
FieldAge = "age"
// FieldName holds the string denoting the name vertex property in the database.
@@ -77,12 +77,12 @@ var (
fields = schema.User{}.Fields()
// descPhone is the schema descriptor for phone field.
descPhone = fields[3].Descriptor()
descPhone = fields[4].Descriptor()
// DefaultPhone holds the default value on creation for the phone field.
DefaultPhone = descPhone.Default.(string)
// descTitle is the schema descriptor for title field.
descTitle = fields[5].Descriptor()
descTitle = fields[6].Descriptor()
// DefaultTitle holds the default value on creation for the title field.
DefaultTitle = descTitle.Default.(string)
)

View File

@@ -21,6 +21,7 @@ import (
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
id *int
age *int
name *string
nickname *string
@@ -120,6 +121,12 @@ func (uc *UserCreate) SetNillableState(u *user.State) *UserCreate {
return uc
}
// SetID sets the id field.
func (uc *UserCreate) SetID(i int) *UserCreate {
uc.id = &i
return uc
}
// AddCarIDs adds the car edge to Car by ids.
func (uc *UserCreate) AddCarIDs(ids ...int) *UserCreate {
if uc.car == nil {
@@ -212,6 +219,10 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
},
}
)
if value := uc.id; value != nil {
u.ID = *value
_spec.ID.Value = *value
}
if value := uc.age; value != nil {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
@@ -328,7 +339,9 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
}
return nil, err
}
id := _spec.ID.Value.(int64)
u.ID = int(id)
if u.ID == 0 {
id := _spec.ID.Value.(int64)
u.ID = int(id)
}
return u, nil
}