我在 SeaORM、Rust 和 Postgres 方面遇到了错误,我是数据库领域的新手,如果这是一个新手问题,那么很抱歉。我想使用 sea-orm-cli 刷新迁移,但收到此错误执行错误:从数据库返回错误:外键约束中引用的列“owner_id”不存在
。
该命令的完整输出如下:
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,
}
我正在关注this official tutorial来自 SeaQL 团队,但我不知道是否有过时的内容,希望您能帮助我。
最佳答案
您在评论表中缺少 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),
)
关于sql - SeaORM 外键约束中引用的列 "owner_id"不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74340776/