You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: SeaORM/docs/04-generate-entity/04-enumeration.md
+43-2Lines changed: 43 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -132,7 +132,21 @@ manager
132
132
```
133
133
134
134
#### 2. `create_enum_from_active_enum`
135
+
This method will provide an interface for adding the type to the database, using the type for table columns, and adding values of this type to rows when seeding data.
@@ -141,12 +155,39 @@ let schema = Schema::new(DbBackend::Postgres);
141
155
142
156
manager
143
157
.create_type(
144
-
// CREATE TYPE "tea" AS ENUM ('EverydayTea', 'BreakfastTea')
145
-
schema.create_enum_from_active_enum::<Tea>(),
158
+
// CREATE TYPE "tea_type" AS ENUM ('EverydayTea', 'BreakfastTea')
159
+
schema.create_enum_from_active_enum::<TeaType>(),
146
160
)
147
161
.await?;
148
162
```
149
163
164
+
##### 3. Use the type as a table column type when creating a table:
165
+
```rust diff
166
+
// in a migration
167
+
168
+
manager::create()
169
+
.table(Tea::Table)
170
+
.if_not_exists()
171
+
.col(Column::new(Tea::Type).custom(TeaType::name())) // use the type for a table column
172
+
// ... more columns
173
+
```
174
+
* see also [Schema Creation Methods - Create Table](https://www.sea-ql.org/SeaORM/docs/migration/writing-migration/#schema-creation-methods)
175
+
176
+
##### 4. Use the type when populating the database:
177
+
```rust
178
+
// in a migration
179
+
180
+
letinsert=Query::insert()
181
+
.into_table(Tea::Table)
182
+
.columns([Tea::TeaType])
183
+
.values_panic([TeaType::EverydayTea.as_enum()]) // call `as_enum` to convert the variant into a SimpleExpr
184
+
.to_owned();
185
+
186
+
manager.exec_stmt(insert).await?;
187
+
// ...
188
+
```
189
+
* see also [Seeding Data - with sea_query statement](https://www.sea-ql.org/SeaORM/docs/migration/seeding-data/#:~:text=write%20SeaQuery%20statement%20to%20seed%20the%20table)
190
+
150
191
## Implementations
151
192
152
193
You can implement [`ActiveEnum`](https://docs.rs/sea-orm/*/sea_orm/entity/trait.ActiveEnum.html) by using the [`DeriveActiveEnum`](https://docs.rs/sea-orm/*/sea_orm/derive.DeriveActiveEnum.html) macro.
0 commit comments