【问题标题】:How to write column names with whitespace in diesel model.rs?如何在柴油 model.rs 中用空格编写列名?
【发布时间】:2020-08-31 23:18:09
【问题描述】:

我正在尝试使用 Diesel 来管理我的数据库以与 Rocket 一起使用,但我一直在为我的表编写 models.rs:

CREATE TABLE `problemSet` (
  `id` varchar(10) NOT NULL,
  `contestId` int DEFAULT NULL,
  `difficulty` varchar(10) DEFAULT NULL,
  `title` varchar(300) DEFAULT NULL,
  `rating` int DEFAULT NULL,
  `link` varchar(300) DEFAULT NULL,
  `binary search` tinyint(1) DEFAULT '0',
  `chinese remainder theorem` tinyint(1) DEFAULT '0',
  `constructive algorithms` tinyint(1) DEFAULT '0',
  `data structures` tinyint(1) DEFAULT '0',
  `dfs and similar` tinyint(1) DEFAULT '0',
  `divide and conquer` tinyint(1) DEFAULT '0',
  PRIMARY KEY(`id`)
  );

在这里,我很困惑如何在 models.rs 的结构中为带有空格的 column_names 编写标识符。

我指的是 Diesel 和 RUST 的官方指南。

【问题讨论】:

    标签: mysql rust rust-diesel rust-rocket


    【解决方案1】:

    在定义架构时,您可以使用sql_name 属性为列指定一个名称,该名称与在 rust 代码中出现的名称不同:

    diesel::table! {
        problemSet {
            ...
            #[sql_name = "divide and conquer"]
            divide_and_conquer -> Text,
            ...
        }
    }
    

    见:documentation for the table! macro

    话虽如此,我的偏好是避免列名中的空格。

    【讨论】:

    • 谢谢@harmic,这很有效,但我认为你关于没有空格的建议要好得多,因此我正在努力。 :)
    猜你喜欢
    • 2020-11-07
    • 2021-09-26
    • 2021-10-07
    • 2020-04-14
    • 2022-12-10
    • 2021-05-08
    • 2021-11-07
    • 2020-08-29
    • 2021-03-17
    相关资源
    最近更新 更多