schema/edge: add support for configuring foreign-key symbols

Fixed #1423
This commit is contained in:
Ariel Mashraki
2021-04-04 14:42:21 +03:00
committed by Ariel Mashraki
parent 745afde770
commit f3f03e1edd
8 changed files with 79 additions and 14 deletions

View File

@@ -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,

View File

@@ -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(),
}
}

View File

@@ -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(),

View File

@@ -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()