mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
schema/edge: add support for configuring foreign-key symbols
Fixed #1423
This commit is contained in:
committed by
Ariel Mashraki
parent
745afde770
commit
f3f03e1edd
@@ -426,7 +426,7 @@ func (g *Graph) Tables() (all []*schema.Table) {
|
||||
OnDelete: deleteAction(e),
|
||||
Columns: []*schema.Column{column},
|
||||
RefColumns: []*schema.Column{ref.PrimaryKey[0]},
|
||||
Symbol: fmt.Sprintf("%s_%s_%s", owner.Name, ref.Name, e.Name),
|
||||
Symbol: fkSymbol(e, owner, ref),
|
||||
})
|
||||
case M2O:
|
||||
ref, owner := tables[e.Type.Table()], tables[e.Rel.Table]
|
||||
@@ -438,7 +438,7 @@ func (g *Graph) Tables() (all []*schema.Table) {
|
||||
OnDelete: deleteAction(e),
|
||||
Columns: []*schema.Column{column},
|
||||
RefColumns: []*schema.Column{ref.PrimaryKey[0]},
|
||||
Symbol: fmt.Sprintf("%s_%s_%s", owner.Name, ref.Name, e.Name),
|
||||
Symbol: fkSymbol(e, owner, ref),
|
||||
})
|
||||
case M2M:
|
||||
t1, t2 := tables[n.Table()], tables[e.Type.Table()]
|
||||
@@ -452,6 +452,7 @@ func (g *Graph) Tables() (all []*schema.Table) {
|
||||
c2.Type = ref.Type.Type
|
||||
c2.Size = ref.size()
|
||||
}
|
||||
s1, s2 := fkSymbols(e, c1, c2)
|
||||
all = append(all, &schema.Table{
|
||||
Name: e.Rel.Table,
|
||||
Columns: []*schema.Column{c1, c2},
|
||||
@@ -462,14 +463,14 @@ func (g *Graph) Tables() (all []*schema.Table) {
|
||||
OnDelete: schema.Cascade,
|
||||
Columns: []*schema.Column{c1},
|
||||
RefColumns: []*schema.Column{t1.PrimaryKey[0]},
|
||||
Symbol: fmt.Sprintf("%s_%s", e.Rel.Table, c1.Name),
|
||||
Symbol: s1,
|
||||
},
|
||||
{
|
||||
RefTable: t2,
|
||||
OnDelete: schema.Cascade,
|
||||
Columns: []*schema.Column{c2},
|
||||
RefColumns: []*schema.Column{t2.PrimaryKey[0]},
|
||||
Symbol: fmt.Sprintf("%s_%s", e.Rel.Table, c2.Name),
|
||||
Symbol: s2,
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -493,6 +494,30 @@ func mayAddColumn(t *schema.Table, c *schema.Column) {
|
||||
}
|
||||
}
|
||||
|
||||
// fkSymbol returns the symbol of the foreign-key constraint for edges of type O2M, M2O and O2O.
|
||||
// It returns the symbol of the storage-key if it was provided, and generate custom one otherwise.
|
||||
func fkSymbol(e *Edge, ownerT, refT *schema.Table) string {
|
||||
if k, _ := e.StorageKey(); k != nil && len(k.Symbols) == 1 {
|
||||
return k.Symbols[0]
|
||||
}
|
||||
return fmt.Sprintf("%s_%s_%s", ownerT.Name, refT.Name, e.Name)
|
||||
}
|
||||
|
||||
// fkSymbols is like fkSymbol but for M2M edges.
|
||||
func fkSymbols(e *Edge, c1, c2 *schema.Column) (string, string) {
|
||||
s1 := fmt.Sprintf("%s_%s", e.Rel.Table, c1.Name)
|
||||
s2 := fmt.Sprintf("%s_%s", e.Rel.Table, c2.Name)
|
||||
if k, _ := e.StorageKey(); k != nil {
|
||||
if len(k.Symbols) > 0 {
|
||||
s1 = k.Symbols[0]
|
||||
}
|
||||
if len(k.Symbols) > 1 {
|
||||
s2 = k.Symbols[1]
|
||||
}
|
||||
}
|
||||
return s1, s2
|
||||
}
|
||||
|
||||
// deleteAction returns the referential action for DELETE operations of the given edge.
|
||||
func deleteAction(e *Edge) schema.ReferenceOption {
|
||||
action := schema.SetNull
|
||||
|
||||
@@ -106,7 +106,7 @@ var (
|
||||
PrimaryKey: []*schema.Column{PetsColumns[0]},
|
||||
ForeignKeys: []*schema.ForeignKey{
|
||||
{
|
||||
Symbol: "pets_users_pets",
|
||||
Symbol: "user_pet_id",
|
||||
Columns: []*schema.Column{PetsColumns[1]},
|
||||
RefColumns: []*schema.Column{UsersColumns[0]},
|
||||
OnDelete: schema.SetNull,
|
||||
@@ -157,13 +157,13 @@ var (
|
||||
PrimaryKey: []*schema.Column{FriendsColumns[0], FriendsColumns[1]},
|
||||
ForeignKeys: []*schema.ForeignKey{
|
||||
{
|
||||
Symbol: "friends_user",
|
||||
Symbol: "user_friend_id1",
|
||||
Columns: []*schema.Column{FriendsColumns[0]},
|
||||
RefColumns: []*schema.Column{UsersColumns[0]},
|
||||
OnDelete: schema.Cascade,
|
||||
},
|
||||
{
|
||||
Symbol: "friends_friend",
|
||||
Symbol: "user_friend_id2",
|
||||
Columns: []*schema.Column{FriendsColumns[1]},
|
||||
RefColumns: []*schema.Column{UsersColumns[0]},
|
||||
OnDelete: schema.Cascade,
|
||||
|
||||
@@ -28,6 +28,7 @@ func (Media) Fields() []ent.Field {
|
||||
// Indexes of the Media.
|
||||
func (Media) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("source", "source_uri").Unique(),
|
||||
index.Fields("source", "source_uri").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,10 +100,14 @@ func (User) Edges() []ent.Edge {
|
||||
edge.To("car", Car.Type),
|
||||
// New edges to added.
|
||||
edge.To("pets", Pet.Type).
|
||||
StorageKey(edge.Column("owner_id")).
|
||||
StorageKey(edge.Column("owner_id"), edge.Symbol("user_pet_id")).
|
||||
Unique(),
|
||||
edge.To("friends", User.Type).
|
||||
StorageKey(edge.Table("friends"), edge.Columns("user", "friend")),
|
||||
StorageKey(
|
||||
edge.Table("friends"),
|
||||
edge.Columns("user", "friend"),
|
||||
edge.Symbols("user_friend_id1", "user_friend_id2"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +126,6 @@ type Car struct {
|
||||
|
||||
func (Car) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
// Car now can have more than 1 owner (not unique anymore).
|
||||
edge.From("owner", User.Type).
|
||||
Ref("car").
|
||||
Unique(),
|
||||
|
||||
@@ -103,6 +103,12 @@ func TestSQLite(t *testing.T) {
|
||||
ContainsFold(t, client)
|
||||
}
|
||||
|
||||
func TestStorageKey(t *testing.T) {
|
||||
require.Equal(t, "user_pet_id", migratev2.PetsTable.ForeignKeys[0].Symbol)
|
||||
require.Equal(t, "user_friend_id1", migratev2.FriendsTable.ForeignKeys[0].Symbol)
|
||||
require.Equal(t, "user_friend_id2", migratev2.FriendsTable.ForeignKeys[1].Symbol)
|
||||
}
|
||||
|
||||
func V1ToV2(t *testing.T, dialect string, clientv1 *entv1.Client, clientv2 *entv2.Client) {
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user