entc/gen: pass referenced columns schema-type to fk columns

Fixed #1699
This commit is contained in:
Ariel Mashraki
2021-07-09 15:30:45 +03:00
committed by Ariel Mashraki
parent 560560a7a0
commit 297067712f
5 changed files with 40 additions and 14 deletions

View File

@@ -418,7 +418,7 @@ func resolve(t *Type) error {
}
// Tables returns the schema definitions of SQL tables for the graph.
func (g *Graph) Tables() (all []*schema.Table) {
func (g *Graph) Tables() (all []*schema.Table, err error) {
tables := make(map[string]*schema.Table)
for _, n := range g.Nodes {
table := schema.NewTable(n.Table()).
@@ -444,7 +444,7 @@ func (g *Graph) Tables() (all []*schema.Table) {
// and "ref" is the referenced table.
owner, ref := tables[e.Rel.Table], tables[n.Table()]
pk := ref.PrimaryKey[0]
column := &schema.Column{Name: e.Rel.Column(), Size: pk.Size, Type: pk.Type, Unique: e.Rel.Type == O2O, Nullable: true}
column := &schema.Column{Name: e.Rel.Column(), Size: pk.Size, Type: pk.Type, Unique: e.Rel.Type == O2O, SchemaType: pk.SchemaType, Nullable: true}
mayAddColumn(owner, column)
owner.AddForeignKey(&schema.ForeignKey{
RefTable: ref,
@@ -456,7 +456,7 @@ func (g *Graph) Tables() (all []*schema.Table) {
case M2O:
ref, owner := tables[e.Type.Table()], tables[e.Rel.Table]
pk := ref.PrimaryKey[0]
column := &schema.Column{Name: e.Rel.Column(), Size: pk.Size, Type: pk.Type, Nullable: true}
column := &schema.Column{Name: e.Rel.Column(), Size: pk.Size, Type: pk.Type, SchemaType: pk.SchemaType, Nullable: true}
mayAddColumn(owner, column)
owner.AddForeignKey(&schema.ForeignKey{
RefTable: ref,

View File

@@ -27,7 +27,7 @@ var (
CardsColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "number", Type: field.TypeString, Nullable: true},
{Name: "owner_id", Type: field.TypeInt, Unique: true, Nullable: true},
{Name: "owner_id", Type: field.TypeInt, Unique: true, Nullable: true, SchemaType: map[string]string{"sqlite3": "integer"}},
}
// CardsTable holds the schema information for the "cards" table.
CardsTable = &schema.Table{
@@ -91,7 +91,7 @@ var (
// PetsColumns holds the columns for the "pets" table.
PetsColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "owner_id", Type: field.TypeInt, Nullable: true},
{Name: "owner_id", Type: field.TypeInt, Nullable: true, SchemaType: map[string]string{"sqlite3": "integer"}},
}
// PetsTable holds the schema information for the "pets" table.
PetsTable = &schema.Table{
@@ -111,7 +111,7 @@ var (
PostsColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "text", Type: field.TypeString},
{Name: "author_id", Type: field.TypeInt, Nullable: true},
{Name: "author_id", Type: field.TypeInt, Nullable: true, SchemaType: map[string]string{"sqlite3": "integer"}},
}
// PostsTable holds the schema information for the "posts" table.
PostsTable = &schema.Table{
@@ -139,7 +139,7 @@ var (
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "date", Type: field.TypeTime},
{Name: "car_id", Type: field.TypeInt, Nullable: true},
{Name: "user_id", Type: field.TypeInt, Nullable: true},
{Name: "user_id", Type: field.TypeInt, Nullable: true, SchemaType: map[string]string{"sqlite3": "integer"}},
}
// RentalsTable holds the schema information for the "rentals" table.
RentalsTable = &schema.Table{
@@ -170,9 +170,9 @@ var (
}
// UsersColumns holds the columns for the "users" table.
UsersColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "parent_id", Type: field.TypeInt, Nullable: true},
{Name: "spouse_id", Type: field.TypeInt, Unique: true, Nullable: true},
{Name: "id", Type: field.TypeInt, Increment: true, SchemaType: map[string]string{"sqlite3": "integer"}},
{Name: "parent_id", Type: field.TypeInt, Nullable: true, SchemaType: map[string]string{"sqlite3": "integer"}},
{Name: "spouse_id", Type: field.TypeInt, Unique: true, Nullable: true, SchemaType: map[string]string{"sqlite3": "integer"}},
}
// UsersTable holds the schema information for the "users" table.
UsersTable = &schema.Table{

View File

@@ -3035,6 +3035,12 @@ func (m UserMutation) Tx() (*Tx, error) {
return tx, nil
}
// SetID sets the value of the id field. Note that this
// operation is only accepted on creation of User entities.
func (m *UserMutation) SetID(id int) {
m.id = &id
}
// ID returns the ID value in the mutation. Note that the ID is only available
// if it was provided to the builder or after it was returned from the database.
func (m *UserMutation) ID() (id int, exists bool) {

View File

@@ -6,6 +6,7 @@ package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
@@ -18,6 +19,11 @@ type User struct {
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id").
SchemaType(map[string]string{
dialect.SQLite: "integer",
}).
Immutable(),
field.Int("parent_id").
Optional(),
field.Int("spouse_id").

View File

@@ -55,6 +55,12 @@ func (uc *UserCreate) SetNillableSpouseID(i *int) *UserCreate {
return uc
}
// SetID sets the "id" field.
func (uc *UserCreate) SetID(i int) *UserCreate {
uc.mutation.SetID(i)
return uc
}
// AddPetIDs adds the "pets" edge to the Pet entity by IDs.
func (uc *UserCreate) AddPetIDs(ids ...int) *UserCreate {
uc.mutation.AddPetIDs(ids...)
@@ -228,8 +234,10 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
if _node.ID == 0 {
id := _spec.ID.Value.(int64)
_node.ID = int(id)
}
return _node, nil
}
@@ -244,6 +252,10 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
},
}
)
if id, ok := uc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
if nodes := uc.mutation.PetsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@@ -441,8 +453,10 @@ func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
}
mutation.id = &nodes[i].ID
mutation.done = true
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(id)
if nodes[i].ID == 0 {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(id)
}
return nodes[i], nil
})
for i := len(builder.hooks) - 1; i >= 0; i-- {