gpt4 book ai didi

postgresql - 插入 Postgres 时无法与类型为 `uuid` 的 Postgres 值相互转换

转载 作者:行者123 更新时间:2023-11-29 08:29:07 25 4
gpt4 key购买 nike

这是我使用 postgres crate(不幸的是 Rust Playground 上没有)将数据插入 Postgres 数据库的代码:

使用以下 Cargo.toml:

[package]
name = "suff-import"
version = "0.1.0"
authors = ["greg"]

[dependencies]
csv = "1"
postgres = "0.15"
uuid = "0.7"

文件main.rs:

extern crate csv;
extern crate postgres;
extern crate uuid;

use uuid::Uuid;
use postgres::{Connection, TlsMode};
use std::error::Error;
use std::io;
use csv::StringRecord;

struct Stuff {
stuff_id: Uuid,
created_at: String,
description: String,
is_something: bool,
}

impl Stuff {
fn from_string_record(record: StringRecord) -> Stuff {
Stuff {
stuff_id: Uuid::parse_str(record.get(0).unwrap()).unwrap(),
created_at: record.get(1).unwrap().to_string(),
description: record.get(2).unwrap().to_string(),
is_something: record.get(3).unwrap().to_string().parse::<i32>().unwrap() == 2,
}
}
}

fn save_row(dbcon: &Connection, stuff: Stuff) -> Result<(), Box<Error>> {
dbcon.execute(
"insert into public.stuff (stuff_id, created_at, description, is_something) values ($1::uuid, $2, $3, $4)",
&[&format!("{}", &stuff.stuff_id).as_str(), &stuff.created_at, &stuff.description, &stuff.is_something]
)?;
Ok(())
}


fn import() -> Result<(), Box<Error>> {
let mut reader = csv::Reader::from_reader(io::stdin());
let dbcon = Connection::connect("postgres://gregoire@10.129.198.251/gregoire", TlsMode::None).unwrap();

for result in reader.records() {
let record = result?;
println!(".");
save_row(&dbcon, Stuff::from_string_record(record))?;
}

Ok(())
}

fn main() {
if let Err(error) = import() {
println!("There were some errors: {}", error);
std::process::exit(1);
}
}

程序编译通过,但运行时出现错误信息退出:

./target/debug/suff-import <<EOF
stuff_id,created_at,description,is_something
5252fff5-d04f-4e0f-8d3e-27da489cf40c,"2019-03-15 16:39:32","This is a description",1
EOF
.
There were some errors: type conversion error: cannot convert to or from a Postgres value of type `uuid`

我测试了使用 format! 宏将 UUID 转换为 &str,因为 Postgres 应该隐式转换为 UUID,但它不起作用(相同的错误消息) .然后我在 Postgres 查询中添加了一个明确的 $1::uuid 但问题仍然存在。

最佳答案

crate page状态:

Optional features

UUID type

UUID support is provided optionally by the with-uuid feature, which adds ToSql and FromSql implementations for uuid's Uuid type. Requires uuid version 0.5.

您没有指定该功能,并且您使用的是不兼容的 uuid 版本。

Cargo.toml

[package]
name = "repro"
version = "0.1.0"
edition = "2018"

[dependencies]
postgres = { version = "0.15.2", features = ["with-uuid"] }
uuid = "0.5"

数据库设置

CREATE TABLE junk (id uuid);

代码

use postgres::{Connection, TlsMode};
use std::error::Error;
use uuid::Uuid;

fn main() -> Result<(), Box<Error>> {
let conn = Connection::connect(
"postgresql://shep@localhost:5432/stackoverflow",
TlsMode::None,
)
.unwrap();

let stuff_id = Uuid::default();

conn.execute(
"insert into public.junk (id) values ($1)",
&[&stuff_id],
)?;

Ok(())
}

另见:

关于postgresql - 插入 Postgres 时无法与类型为 `uuid` 的 Postgres 值相互转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55219102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com