mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql/schema: setrange on custom column name of pks (#333)
This commit is contained in:
@@ -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},
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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},
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user