柴油迁移力字符串而不是schema.rs中的uuid

发布于 2025-02-13 15:39:51 字数 778 浏览 0 评论 0原文

我对柴油迁移有问题。我需要将UUID作为模型的主要密钥实现。我在UUID集成方面有很多问题(功能UUIDV07,UUID板条箱,..)我的模型中的一种字段。

users.sql

CREATE TABLE users (
  id UUID PRIMARY KEY,
  email VARCHAR NOT NULL,
  name VARCHAR NOT NULL,
  password VARCHAR NOT NULL,
  id_role INT,
  CONSTRAINT fk_role
    FOREIGN KEY(id_role)
      REFERENCES roles(id)
)

schema.rs


table! {
    users (id) {
        id -> Varchar,
        email -> Varchar,
        name -> Varchar,
        password -> Varchar,
        id_role -> Nullable<Int4>,
    }
}

使用varchar而不是uuid是正常的吗?

uuid = { version = "0.8.2", features = ["serde", "v4"] }
diesel = { version = "1.4.5", features = ["postgres", "r2d2", "uuidv07"] }

谢谢。

I have a problem with the diesel migration. I need to implement Uuid as primary key for a model. I got a lot of issues with the Uuid integration (feature uuidv07, uuid crate,..) but when I specify the type uuid in the migration, diesel generate a "Varchar" field in the migration, so I can't use Uuid as a type of field in my model.

users.sql

CREATE TABLE users (
  id UUID PRIMARY KEY,
  email VARCHAR NOT NULL,
  name VARCHAR NOT NULL,
  password VARCHAR NOT NULL,
  id_role INT,
  CONSTRAINT fk_role
    FOREIGN KEY(id_role)
      REFERENCES roles(id)
)

schema.rs


table! {
    users (id) {
        id -> Varchar,
        email -> Varchar,
        name -> Varchar,
        password -> Varchar,
        id_role -> Nullable<Int4>,
    }
}

Is this normal to use Varchar and not Uuid ?

uuid = { version = "0.8.2", features = ["serde", "v4"] }
diesel = { version = "1.4.5", features = ["postgres", "r2d2", "uuidv07"] }

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥繁 2025-02-20 15:39:51

柴油不会将任何东西迫使您。它只是从数据库系统中读取类型。因此,如果它输出varchar作为特定列的类型,则表示您的数据库系统记录varchar(出于何种原因)作为此列的类型。或对此有所不同:这里的问题不是柴油,而是您的迁移,现有数据库架构和数据库之间的交互。作为您的问题架构或有关数据库系统的有关问题的一部分,我均未提供有关您的数据库系统的信息,因此我在这里没有太多的信息可以将您指向正确的方向。

Diesel does not force anything onto you. It just reads the types from your database system. So if it outputs a Varchar as type for a specific column that means your database system recorded Varchar (for whatever reason) as type for this column. Or to word it differently: The problem here is not diesel, but likely an interaction between your migration, your existing database schema and your database. As neither information about your pre-existing database schema nor about your database system is provided as part of your question there is not much I can add here to point you into the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文