Skip to content

Commit

Permalink
Add Schema::create_index_from_entity (SeaQL/sea-orm#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Mar 25, 2022
1 parent 4a21d17 commit 29961fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SeaORM/docs/01-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ If you prefer step-by-step tutorials on how to use SeaORM. You can checkout [Sea

3.2 [Create Enum](04-generate-database-schema/02-create-enum.md)

3.3 [Create Index](04-generate-database-schema/03-create-index.md)

4. Migration

4.1 [Setting Up Migration](05-migration/01-setting-up-migration.md)
Expand Down
2 changes: 2 additions & 0 deletions SeaORM/docs/04-generate-database-schema/01-create-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ To create tables in database instead of writing [`TableCreateStatement`](https:/

Below we use [`CakeFillingPrice`](https://github.com/SeaQL/sea-orm/blob/master/src/tests_cfg/cake_filling_price.rs) entity to demo its generated SQL statement. You can construct the same statement with [`TableCreateStatement`](https://docs.rs/sea-query/*/sea_query/table/struct.TableCreateStatement.html).

Note that since version `0.7.0`, [`Schema::create_table_from_entity`](https://docs.rs/sea-orm/0.5/sea_orm/schema/struct.Schema.html#method.create_table_from_entity) no longer create indexes. If you need to create indexes in database please check [here](04-generate-database-schema/03-create-index.md) for details.

```rust
use sea_orm::{sea_query::*, tests_cfg::*, EntityName, Schema};

Expand Down
29 changes: 29 additions & 0 deletions SeaORM/docs/04-generate-database-schema/03-create-index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Create Index

To create indexes in database instead of writing [`IndexCreateStatement`](https://docs.rs/sea-query/*/sea_query/index/struct.IndexCreateStatement.html) manually, you can derive it from `Entity` using [`Schema::create_index_from_entity`](https://docs.rs/sea-orm/0.5/sea_orm/schema/struct.Schema.html#method.create_index_from_entity). This method will help you create database indexes defined in `Entity`.

Below we use [`Indexes`](https://github.com/SeaQL/sea-orm/blob/master/src/tests_cfg/indexes.rs) entity to demo its generated SQL statement. You can construct the same statement with [`IndexCreateStatement`](https://docs.rs/sea-query/*/sea_query/index/struct.IndexCreateStatement.html).

```rust
use sea_orm::{sea_query, tests_cfg::*, Schema};

let builder = db.get_database_backend();
let schema = Schema::new(builder);

let stmts = schema.create_index_from_entity(indexes::Entity);
assert_eq!(stmts.len(), 2);

let idx = sea_query::Index::create()
.name("idx-indexes-index1_attr")
.table(indexes::Entity)
.col(indexes::Column::Index1Attr)
.to_owned();
assert_eq!(builder.build(&stmts[0]), builder.build(&idx));

let idx = sea_query::Index::create()
.name("idx-indexes-index2_attr")
.table(indexes::Entity)
.col(indexes::Column::Index2Attr)
.to_owned();
assert_eq!(builder.build(&stmts[1]), builder.build(&idx));
```

0 comments on commit 29961fc

Please sign in to comment.