migration error--beacause the existing data table. #1632
Replies: 4 comments
-
|
Hey @Tom-debug110, thanks for sharing |
Beta Was this translation helpful? Give feedback.
-
|
I'm sorry to bring up this issue again after so long. I just started using migration. Keep getting this error. I think has data table is not the key reason , I deleted the SQLite database and still get errors. My entity #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "reading_histories")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(indexed)]
pub novel_id: String,
pub novel_name: String,
pub chapter_id: String,
pub chapter_title: String,
pub last_read_at: i64,
pub progress: i32, // 阅读进度 0-1
}Migration #[derive(DeriveMigrationName)]
struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(
&self,
manager: &SchemaManager,
) -> std::result::Result<(), sea_orm_migration::DbErr> {
let db = manager.get_connection();
let backend = db.get_database_backend();
let schema = Schema::new(backend);
manager
.create_table(
schema
.create_table_from_entity(Entity)
.if_not_exists()
.to_owned(),
)
.await?;
Ok(())
}
async fn down(
&self,
manager: &SchemaManager,
) -> std::result::Result<(), sea_orm_migration::DbErr> {
Ok(())
}
}
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(Migration),
]
}
}call Migrator fn create_conn(){
let properties_url = format!("sqlite:{}?mode=rwc", "properties");
let properties_db = Database::connect(&properties_url).await?;
}
pub(crate) async fn execute_migrations() -> Result<()> {
let conn = get_connect().await;
migrations::m20250507_1423_create_table_reading_history::Migrator::up(conn, None).await?;
Ok(())
}cargo.toml |
Beta Was this translation helpful? Give feedback.
-
|
clean code only migration. Just run it and it will reappear. Can someone help me? |
Beta Was this translation helpful? Give feedback.
-
|
Problem has been resolved, multiple migrators should not be used. If two migrations are placed in one migrator, this error will not occur. maybe, in the seaql_migrations table have a version that does not exist in the current migrator, this error will occur. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
An error occurred while running the introductory example for automated migration.
such as:
Runningcargo run --manifest-path ./migration/Cargo.toml -- up -u mysql://root:12345@localhost:3307/devFinished dev [unoptimized + debuginfo] target(s) in 0.37s Runningmigration/target/debug/migration up -u 'mysql://root:12345@localhost:3307/dev'Applying all pending migrations Custom Error: Migration file of version 'm20220120_000001_create_post_table' is missing, this migration has been applied but its file is missing Fail to run migrationInitially thought it was a version issue, attempted to downgrade versions, but the issue still persisted.
finally, the reason is the database dev has a existing table named posts, and save a seaql-migrations table to record some useful infomation. and the content of this table contains a migration table name, a bigint.
so, I create a new database to run this little demo, every thing is ok!.
Summary: When using sea-orm, it is recommended to use a new database(has no any table), especially for beginners who are just starting to learn sea-orm.
Beta Was this translation helpful? Give feedback.
All reactions