sql - SeaORM 外键约束中引用的列 "owner_id"不存在

标签 sql postgresql rust sea-orm

我在 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/

相关文章:

postgresql - 使用KML生成几何图形

rust - 我可以在不包装浮点类型和滥用 BTreeMap 的情况下使用标准库执行二叉树搜索吗?

sql - 在 Postgresql 查询中定义“强制性或非强制性”的标准是什么?

python - 如何使用 pyspark 在 Spark 2.0 中构建 sparkSession?

sql - 如何在 ActiveRecord 中为 SUM 添加 where 作用域?

sql - CASE WHEN 与 OR

rust - Rust 编译器在多大程度上自动匹配泛型约束?

arrays - 如何从函数返回固定大小的字符串文字数组?

SQL Server 算术溢出错误将 IDENTITY 转换为数据类型 int 但尚未达到 int 的最大值

mysql - 如何计算 MySQL 中 NULL 运行的运行长度?