我對 SeaORM、Rust 和 Postgres 有一個錯誤,我是資料庫領域的新手,如果這是一個新手問題,我很抱歉。我想用 sea-orm-cli 重繪 遷移,但我收到了這個錯誤Execution Error: error returned from database: column "owner_id" referenced in foreign key constraint does not exist。
該命令的完整輸出如下:
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
我認為這可能是我名為m20221106_182552_create_comment_table.rs.
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,
}
我正在關注SeaQL 團隊的這個官方教程,但我不知道是否有過時的東西,希望你能幫助我。
uj5u.com熱心網友回復:
您在評論表中缺少 OwnerId 列。您必須先創建它,然后在其上創建 FK 約束。
見教程中有
.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),
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/528920.html
上一篇:獲取sql中行的差異總和
