添加外键到现有表diesel Rust。

huangapple go评论65阅读模式
英文:

Add foreign key to the existing table diesel Rust

问题

我有两张现有的表格:user_table和post。我需要建立这两张表格之间的关系,即将user_id添加到post表格中。
我有以下的迁移代码:

ALTER TABLE post
ADD CONSTRAINT fk_post_user_id
FOREIGN KEY (user_id)
REFERENCES user_table (id);

数据库结构:

diesel::table! {
    post (id) {
        id -> Int4,
        title -> Nullable<Varchar>,
        content -> Nullable<Varchar>,
        date -> Timestamp,
        user_id -> Int4,
    }
}

diesel::table! {
    user_table (id) {
        id -> Int4,
        username -> Varchar,
        email -> Varchar,
        password -> Varchar,
    }
}

diesel::joinable!(post -> user_table (user_id));

diesel::allow_tables_to_appear_in_same_query!(
    post,
    user_table,
);

我自己添加的这两行代码:

diesel::joinable!(post -> user_table (user_id));
user_id -> Int4

我的post和user模型:

#[derive(Queryable, Identifiable, Associations)]
#[belongs_to(User)]
#[table_name="post"]
pub struct Post {
    pub id: i32,
    pub title: String,
    pub content: String,
    pub user_id: i32
}

#[derive(Queryable, Identifiable, Clone)]
#[table_name="user_table"]
pub struct User {
    pub id: i32,
    pub username: String,
    pub email: String,
    pub password: String,
}

创建表格的SQL查询:

CREATE TABLE user_table (
    id SERIAL PRIMARY KEY,
    username VARCHAR NOT NULL UNIQUE,
    email VARCHAR NOT NULL UNIQUE,
    password VARCHAR NOT NULL
);
CREATE TABLE post (
    id SERIAL PRIMARY KEY,
    title VARCHAR,
    content VARCHAR,
    date timestamp NOT NULL DEFAULT NOW()
);

我遇到了一个迁移错误:

列"user_id"在外键约束中引用,但不存在

我是后端开发的新手,卡在了这个问题上。非常感谢任何支持。

英文:

I have two existing tables: user_table and post. I need to make the relation between these two tables i.e. add user_id to the post table.
I have the following migration

ALTER TABLE post
ADD CONSTRAINT fk_post_user_id
FOREIGN KEY (user_id)
REFERENCES user_table (id);

Schema

diesel::table! {
    post (id) {
        id -&gt; Int4,
        title -&gt; Nullable&lt;Varchar&gt;,
        content -&gt; Nullable&lt;Varchar&gt;,
        date -&gt; Timestamp,
        user_id -&gt; Int4,
    }
}

diesel::table! {
    user_table (id) {
        id -&gt; Int4,
        username -&gt; Varchar,
        email -&gt; Varchar,
        password -&gt; Varchar,
    }
}

diesel::joinable!(post -&gt; user_table (user_id));

diesel::allow_tables_to_appear_in_same_query!(
    post,
    user_table,
);

These two lines I added by myself

diesel::joinable!(post -&gt; user_table (user_id));
user_id -&gt; Int4

My post and user models

#[derive(Queryable, Identifiable, Associations)]
#[belongs_to(User)]
#[table_name=&quot;post&quot;]
pub struct Post {
    pub id: i32,
    pub title: String,
    pub content: String,
    pub user_id: i32
}

#[derive(Queryable, Identifiable, Clone)]
#[table_name=&quot;user_table&quot;]
pub struct User {
    pub id: i32,
    pub username: String,
    pub email: String,
    pub password: String,
}

SQL queries for creating tables

CREATE TABLE user_table (
    id SERIAL PRIMARY KEY,
    username VARCHAR NOT NULL UNIQUE,
    email VARCHAR NOT NULL UNIQUE,
    password VARCHAR NOT NULL
);
CREATE TABLE post (
    id SERIAL PRIMARY KEY,
    title VARCHAR,
    content VARCHAR,
    date timestamp NOT NULL DEFAULT NOW()
);

I am getting a migration error

> column "user_id" referenced in foreign key constraint does not exist

I am new to the backend development and I am stuck with this problem.
Would be grateful for any support

答案1

得分: 1

关于您的迁移问题:

ALTER TABLE post
ADD CONSTRAINT fk_post_user_id
FOREIGN KEY (user_id)
REFERENCES user_table (id);

这基本上是在现有的列 user_id 上创建一个外键约束,该约束引用了另一个表,但是 user_id 列不存在。您可以通过在迁移之前添加 ALTER TABLE posts ADD COLUMN user_id INTEGER; 来解决这个问题,以创建 user_id 列。

英文:

The issue in with your migration:

ALTER TABLE post
ADD CONSTRAINT fk_post_user_id
FOREIGN KEY (user_id)
REFERENCES user_table (id);

This basically says create a foreign key constraint on the existing column user_id that references the other table, but the user_id column does not exist. You can fix that by adding a ALTER TABLE posts ADD COLUMN user_id INTEGER; to your migration before creating the foreign key.

答案2

得分: 0

user_id直接添加到posts表中:


CREATE TABLE user_table (
    id SERIAL PRIMARY KEY,
    username VARCHAR NOT NULL UNIQUE,
    email VARCHAR NOT NULL UNIQUE,
    password VARCHAR NOT NULL
);
CREATE TABLE post (
    id SERIAL PRIMARY KEY,
    title VARCHAR,
    content VARCHAR,
    user_id INT,
    date timestamp NOT NULL DEFAULT NOW()
);


如果设置了此模式,外键约束将起作用。

请记得删除迁移(删除表)并重新运行迁移。

如果你的表中有一些数据,请小心,如果你删除表,数据将丢失。

英文:

I would suggest directly adding user_id

To posts table:


CREATE TABLE user_table (
    id SERIAL PRIMARY KEY,
    username VARCHAR NOT NULL UNIQUE,
    email VARCHAR NOT NULL UNIQUE,
    password VARCHAR NOT NULL
);
CREATE TABLE post (
    id SERIAL PRIMARY KEY,
    title VARCHAR,
    content VARCHAR,
    user_id INT,
    date timestamp NOT NULL DEFAULT NOW()
);


The FK constraint will work if this schema is set.

Remember to drop migrations ( drop the tables) and re-run the migrations.

Be careful if you have some data in your tables. If you drop a table data will be lost.

huangapple
  • 本文由 发表于 2023年7月20日 17:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定