I have an error with SeaORM, Rust and Postgres, I'm new in the world of the databases so sorry if this is a newbie question. I wanted to refresh the migrations with sea-orm-cli, but i got this error Execution Error: error returned from database: column "owner_id" referenced in foreign key constraint does not exist
.
the full output of the command is this one:
Running `cargo run --manifest-path ./migration/Cargo.toml -- fresh -u postgres://postgres@localhost:5432/task_manager`
Finished dev [unoptimized debuginfo] target(s) in 0.54s
Running `migration/target/debug/migration fresh -u 'postgres://postgres@localhost:5432/task_manager'`
Dropping table 'seaql_migrations'
Table 'seaql_migrations' has been dropped
Dropping table 'owner'
Table 'owner' has been dropped
Dropping all types
Applying all pending migrations
Applying migration 'm20221106_182043_create_owner_table'
Migration 'm20221106_182043_create_owner_table' has been applied
Applying migration 'm20221106_182552_create_comment_table'
Execution Error: error returned from database: column "owner_id" referenced in foreign key constraint does not exist
I thought it could be some problem with my code in the file called m20221106_182552_create_comment_table.rs
, but I took a look to it and it doesn't seems to be a problem.
use sea_orm_migration::prelude::*;
use super::m20221106_182043_create_owner_table::Owner;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Comment::Table)
.if_not_exists()
.col(
ColumnDef::new(Comment::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Comment::Url).string().not_null())
.col(ColumnDef::new(Comment::Body).string().not_null())
.col(ColumnDef::new(Comment::IssueUrl).string().not_null())
.col(ColumnDef::new(Comment::CreatedAt).string())
.col(ColumnDef::new(Comment::UpdatedAt).string())
.foreign_key(
ForeignKey::create()
.name("fk-comment-owner_id")
.from(Comment::Table, Comment::OwnerId)
.to(Owner::Table, Owner::Id),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Comment::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Comment {
Table,
Id,
Url,
Body,
IssueUrl,
CreatedAt,
UpdatedAt,
OwnerId,
}
I was following this official tutorial from the SeaQL team but I don't know if there is something outdated, hope you can help me with this.
CodePudding user response:
You are missing the OwnerId column in the comments table. You have to create it first and then create the FK constraint on it.
See in the tutorial there is
.col(ColumnDef::new(Chef::BakeryId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-chef-bakery_id")
.from(Chef::Table, Chef::BakeryId)
.to(Bakery::Table, Bakery::Id),
)