简体   繁体   English

创建的用户和类别表,created_by,modified_by 作为用户的外键,架构不包含可连接的 stm,柴油 rust Postgres

[英]created users & categories tables with created_by,modified_by as foreignkeys on users, schema doesn't contain joinable stm , diesel rust Postgres

I have 2 issues.我有2个问题。

  1. There is no joinable.没有可加入的。 entry for "categories" in the schema,rs?架构中“类别”的条目,rs? why does this happen?为什么会这样?

  2. if 2 columns are added with foreign key reference to the same table, it doesn't allow me to add two joinable, statements manually: eg:如果 2 列添加了对同一个表的外键引用,它不允许我手动添加两个可连接的语句:例如:

    joinable;(categories -> users (created_by));可加入;(类别 -> 用户(created_by)); joinable!(categories -> users (modified_by));可加入!(类别 -> 用户(modified_by));

I tried creating the "categories" table with one foreign key reference to the "users" table, dropped database, deleted schema.rs, ran migration, still no change to the schema.我尝试使用对“用户”表的一个外键引用创建“类别”表,删除数据库,删除 schema.rs,运行迁移,仍然没有更改架构。

table! {
    categories (id) {
        id -> Int4,
        category_code -> Varchar,
        description -> Varchar,
        gl_cr -> Varchar,
        gl_dr -> Varchar,
        asset_cost_less_than -> Nullable<Int4>,
        over_ride_depreciation_rate -> Bool,
        over_ride_useful_life -> Bool,
        created_by -> Int4,
        created_on -> Timestamp,
        modified_by -> Nullable<Int4>,
        modified_date -> Nullable<Timestamp>,
    }
}


table! {
    users (id) {
        id -> Int4,
        username -> Varchar,
        full_name -> Varchar,
        email -> Varchar,
        active -> Bool,
        created_on -> Timestamp,
    }
}

part of the schema.rs is...... schema.rs 的一部分是......

joinable!(branch_categories -> categories (category_id));
joinable!(branches -> cities (city_id));
joinable!(branches -> users (created_by));
joinable!(category_book_rate_codes -> books (book_id));
joinable!(category_book_rate_codes -> categories (category_id));
joinable!(category_book_rate_codes -> depreciation_rates (rate_id));
joinable!(cities -> states (state_id));
joinable!(cities -> users (created_by));

..... ......

If your categories table has two columns that reference the users table:如果您的categories表有两列引用users表:

CREATE TABLE categories (
    id           INTEGER,
    created_by   INTEGER NOT NULL REFERENCES users,
    modified_by  INTEGER          REFERENCES users,
    ...
);

Diesel will not generate a joinable! Diesel 不会生成joinable! entry for categories to users since it would be ambiguous which column to use.users输入categories ,因为使用哪一列会不明确。 You cannot create them both manually either because their implementations would conflict.您也不能手动创建它们,因为它们的实现会发生冲突。 The macro is designed to make this kind of query possible without an explicit ON clause:该宏旨在使这种查询在没有显式ON子句的情况下成为可能:

categories::table.join(users::table)...

Not sure why removing one foreign reference and regenerating the schema didn't make a difference for you, but regardless... You can of course implement one of them manually like so:不知道为什么删除一个外部引用并重新生成架构对您没有任何影响,但无论如何......您当然可以像这样手动实现其中一个

joinable!(categories -> users (created_by));

You could then use the above query syntax when you wanted to join categories to users based on their creator.然后,当您想根据创建者将categories加入users时,可以使用上述查询语法。 If you later want to join them based on their last modifier as well, you can do so in your query explicitly (untested):如果您以后还想根据它们的最后一个修饰符加入它们,您可以在查询中明确地这样做(未经测试):

categories::table.join(users::table.on(categories::modified_by.eq(users::id.nullable())))...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM