在前兩篇的文章中,其實對於索引也有一些使用, 這裏來詳細看一下關於索引的使用git
Indexes方法能夠在一個或者多個字段上設置索引,以提升數據檢索的速度或者定義數據的惟一性github
在下面這個例子中,對user表的field1
和field2
字段設置了聯合索引;對first_name
和last_name
設置了聯合惟一索引; 對field3
設置了惟一索引。sql
這裏須要注意對於單獨的字段設置惟一索引,在Fields中定義字段的時候經過Unique方法便可ui
package schema import ( "github.com/facebook/ent" "github.com/facebook/ent/schema/field" "github.com/facebook/ent/schema/index" ) // User holds the schema definition for the User entity. type User struct { ent.Schema } // Fields of the User. func (User) Fields() []ent.Field { return []ent.Field{ field.String("field1"), field.String("field2"), field.String("field3").Unique(), field.String("first_name"), field.String("last_name"), } } // Edges of the User. func (User) Edges() []ent.Edge { return nil } func Indexes() []ent.Index { return []ent.Index{ // non-unique index. index.Fields("field1", "field2"), // unique index index.Fields("first_name", "last_name").Unique(), } }
查看一下生成的表的信息:rest
CREATE TABLE `users` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `field1` varchar(255) COLLATE utf8mb4_bin NOT NULL, `field2` varchar(255) COLLATE utf8mb4_bin NOT NULL, `field3` varchar(255) COLLATE utf8mb4_bin NOT NULL, `first_name` varchar(255) COLLATE utf8mb4_bin NOT NULL, `last_name` varchar(255) COLLATE utf8mb4_bin NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `field3` (`field3`), UNIQUE KEY `user_first_name_last_name` (`first_name`,`last_name`), KEY `user_field1_field2` (`field1`,`field2`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
在創建表關係的時候也能夠對相應的字段設置索引,主要實在特定關係下設置字段的惟一性code
在這個例子中,咱們有一個有許多街道的城市,咱們但願在每一個城市下設置街道名稱是惟一的。blog
ent/schema/city.go
索引
package schema import ( "github.com/facebook/ent" "github.com/facebook/ent/schema/edge" "github.com/facebook/ent/schema/field" ) // City holds the schema definition for the City entity. type City struct { ent.Schema } // Fields of the City. func (City) Fields() []ent.Field { return []ent.Field{ field.String("name"), } } // Edges of the City. func (City) Edges() []ent.Edge { return []ent.Edge{ edge.To("streets", Street.Type), } }
ent/schema/street.go
接口
package schema import ( "github.com/facebook/ent" "github.com/facebook/ent/schema/edge" "github.com/facebook/ent/schema/field" ) // Street holds the schema definition for the Street entity. type Street struct { ent.Schema } // Fields of the Street. func (Street) Fields() []ent.Field { return []ent.Field{ field.String("name"), } } // Edges of the Street. func (Street) Edges() []ent.Edge { return []ent.Edge{ edge.From("city", City.Type). Ref("streets"). Unique(), } }
在上一篇文章中這種用法咱們已經見過,咱們看一下這樣建立的表信息,主要是看streets這個表:ci
CREATE TABLE `streets` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8mb4_bin NOT NULL, `city_streets` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`), KEY `streets_cities_streets` (`city_streets`), CONSTRAINT `streets_cities_streets` FOREIGN KEY (`city_streets`) REFERENCES `cities` (`id`) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
在ent/schema/street.go
添加索引的信息後,再次查看streets表的信息,其實這裏咱們就是經過添加約束,使得一個城市中每一個街道的名稱是惟一的
func (Street) Indexes() []ent.Index { return []ent.Index{ index.Fields("name"). Edges("city"). Unique(), } }
CREATE TABLE `streets` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8mb4_bin NOT NULL, `city_streets` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `street_name_city_streets` (`name`,`city_streets`), KEY `streets_cities_streets` (`city_streets`), CONSTRAINT `streets_cities_streets` FOREIGN KEY (`city_streets`) REFERENCES `cities` (`id`) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
能夠使用Table選項爲類型提供自定義表名,以下所示:
package schema import ( "github.com/facebook/ent" "github.com/facebook/ent/schema/field" ) type User struct { ent.Schema } func (User) Config() ent.Config { return ent.Config{ Table: "Users", } } func (User) Fields() []ent.Field { return []ent.Field{ field.Int("age"), field.String("name"), } }
Mixin容許您建立可重用的ent.Schema
代碼。ent.Mixin
接口以下:
Mixin interface { // Fields returns a slice of fields to add to the schema. Fields() []Field // Edges returns a slice of edges to add to the schema. Edges() []Edge // Indexes returns a slice of indexes to add to the schema. Indexes() []Index // Hooks returns a slice of hooks to add to the schema. // Note that mixin hooks are executed before schema hooks. Hooks() []Hook }
Mixin的一個常見用例是將一些經常使用的通用字段進行內置,以下:
package schema import ( "time" "github.com/facebook/ent" "github.com/facebook/ent/schema/field" "github.com/facebook/ent/schema/mixin" ) // ------------------------------------------------- // Mixin definition // TimeMixin implements the ent.Mixin for sharing // time fields with package schemas. type TimeMixin struct{ // We embed the `mixin.Schema` to avoid // implementing the rest of the methods. mixin.Schema } func (TimeMixin) Fields() []ent.Field { return []ent.Field{ field.Time("created_at"). Immutable(). Default(time.Now), field.Time("updated_at"). Default(time.Now). UpdateDefault(time.Now), } } // DetailsMixin implements the ent.Mixin for sharing // entity details fields with package schemas. type DetailsMixin struct{ // We embed the `mixin.Schema` to avoid // implementing the rest of the methods. mixin.Schema } func (DetailsMixin) Fields() []ent.Field { return []ent.Field{ field.Int("age"). Positive(), field.String("name"). NotEmpty(), } } // ------------------------------------------------- // Schema definition // User schema mixed-in the TimeMixin and DetailsMixin fields and therefore // has 5 fields: `created_at`, `updated_at`, `age`, `name` and `nickname`. type User struct { ent.Schema } func (User) Mixin() []ent.Mixin { return []ent.Mixin{ TimeMixin{}, DetailsMixin{}, } } func (User) Fields() []ent.Field { return []ent.Field{ field.String("nickname"). Unique(), } } // Pet schema mixed-in the DetailsMixin fields and therefore // has 3 fields: `age`, `name` and `weight`. type Pet struct { ent.Schema } func (Pet) Mixin() []ent.Mixin { return []ent.Mixin{ DetailsMixin{}, } } func (Pet) Fields() []ent.Field { return []ent.Field{ field.Float("weight"), } }
Mixin
提供了一些內置的Mixin
,可用於將create_time和update_time字段添加到schema中。
爲了使用它們,將 mixin.Time mixin
添加到schema,以下所示:
package schema import ( "github.com/facebook/ent" "github.com/facebook/ent/schema/mixin" ) type Pet struct { ent.Schema } func (Pet) Mixin() []ent.Mixin { return []ent.Mixin{ mixin.Time{}, // Or, mixin.CreateTime only for create_time // and mixin.UpdateTime only for update_time. } }