seaql.github.io
seaql.github.io copied to clipboard
Fix Create Data Type section in 02-writing-migration.md
This is to fix 2 compile errors.
- Add Type import to fix the following error:
- error[E0433]: failed to resolve: use of undeclared type
Type
- error[E0433]: failed to resolve: use of undeclared type
- Add
to_owned()method to fix the following error:- expected
TypeCreateStatement, found&mut TypeCreateStatement
- expected
Changes
- [x] Fixes 2 compile errors in sample code in document.
Sea-orm version
[dependencies.sea-orm-migration]
version = "~1.0"
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
# e.g.
"runtime-tokio-native-tls", # `ASYNC_RUNTIME` feature
"sqlx-postgres", # `DATABASE_DRIVER` feature
"with-uuid",
"with-chrono",
"with-json",
]
About the first error. Code
use sea_orm::{EnumIter, Iterable};
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_type(
Type::create()
.as_enum(TimezoneEnum)
.values(TimezoneVariants::iter()),
)
.await
}
}
#[derive(DeriveIden)]
struct TimezoneEnum;
#[derive(DeriveIden, EnumIter)]
pub enum TimezoneVariants {
#[sea_orm(iden = "Asia/Tokyo")]
AsiaTokyo,
#[sea_orm(iden = "UTC")]
Utc,
}
Error message on sea-orm-cli migrate fresh
Running `cargo run --manifest-path ./migration/Cargo.toml -- fresh -u postgresql://test_user:xxxxxx@localhost:5432/test`
Compiling migration v0.1.0 (/migration)
error[E0433]: failed to resolve: use of undeclared type `Type`
--> src/m20240926_000001_create_users_table.rs:12:17
|
12 | Type::create()
| ^^^^ use of undeclared type `Type`
|
help: consider importing one of these items
|
1 + use crate::extension::postgres::Type;
|
1 + use sea_orm_migration::prelude::extension::postgres::Type;
|
For more information about this error, try `rustc --explain E0433`.
error: could not compile `migration` (lib) due to 1 previous error
Fail to run migration
The second error. Code:
use sea_orm_migration::prelude::extension::postgres::Type;
// No other changes to the code above.
Error:
Running `cargo run --manifest-path ./migration/Cargo.toml -- fresh -u postgresql://test_user:xxxxxx@localhost:5432/test`
Compiling migration v0.1.0 (/migration)
error[E0308]: mismatched types
--> src/m20240926_000001_create_users_table.rs:13:17
|
12 | .create_type(
| ----------- arguments to this method are incorrect
13 | / Type::create()
14 | | .as_enum(TimezoneEnum)
15 | | .values(TimezoneVariants::iter()),
| |_____________________________________________________^ expected `TypeCreateStatement`, found `&mut TypeCreateStatement`
|
note: method defined here
--> /.cargo/registry/src/index.crates.io-6f17d22bba15001f/sea-orm-migration-1.0.0/src/manager.rs:57:18
|
57 | pub async fn create_type(&self, stmt: TypeCreateStatement) -> Result<(), DbErr> {
| ^^^^^^^^^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `migration` (lib) due to 1 previous error
Fail to run migration