document: https://diesel.rs/guides/getting-started/ https://www.rectcircle.cn/posts/rust-diesel/

install:

1
2
// 只安装pg依赖
cargo install diesel_cli --no-default-features --features postgres

配置文件

1
2
3
# echo DATABASE_URL=postgres://username:password@localhost/diesel_demo > .env

echo DATABASE_URL=postgres://localhost/test?user=root&password=example > .env

cargo.toml 目前只支持uuid 0.6

1
2
3
4
diesel = { version = "1.4.4", features = ["postgres", "numeric", "chrono", "uuid"] }
bigdecimal = "0.0.14"
chrono = "0.4"
uuid = "0.6"

setup生成目录migrations

1
diesel setup

When we ran diesel setup, a file called diesel.toml was created which tells Diesel to maintain a file at src/schema.rs for us. setup不但会生成migrations而且会生成src/schema.rs和diesel.toml文件

生成create_posts的up.sql和down.sql

1
diesel migration generate create_posts

在库里建表

在up.sql中添加建表语句后,执行:

1
diesel migration run

用于生成schema.rs(可用于从库里同步下来结构)

添加依赖

以uuid为例: cargo.toml修改:

1
2
diesel = { version = "1.4.4", features = ["postgres", "numeric", "chrono", "uuid"] }
uuid = "0.6"

model.rs中添加:

1
2
extern crate uuid;
use uuid::Uuid;

diesel migration redo = diesel migration revert && diesel migration run

query总是无法clone

https://discourse.diesel.rs/t/conditional-query-issues-with-boxing-and-deleting/96/2

box

https://users.rust-lang.org/t/diesel-conditional-query-builder/15815

https://stackoverflow.com/questions/56600021/how-to-clone-a-boxcustomstruct-when-i-keep-getting-no-method-named-clone

box的clone问题:

https://stackoverflow.com/questions/30353462/how-to-clone-a-struct-storing-a-boxed-trait-object 思路: 自定义clone

type-map

https://kotiri.com/2018/01/31/postgresql-diesel-rust-types.html

Updated as at Postgresql 10.1, Diesel 1.1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
PostgreSQL Type	Description	Diesel Type	Rust Type
Nullable Types	nullable	Nullable<T>	Option<T>
Numeric Types	 	 	 
smallint, int2	signed integer	SmallInt	i16
integer, int, int4	signed integer	Integer	i32
bigint, int8	signed integer	BigInt	i64
numeric(p, s), decimal(p, s)	exact numeric of selectable precision	Numeric	bigdecimal::BigDecimal
real, float4	single precision floating-point number	Float	f32
double precision, float8	double precision floating-point number	Double	f64
smallserial, serial2	autoincrementing integer	SmallInt	i16
serial, serial4	autoincrementing integer	Integer	i32
bigserial, serial8	autoincrementing integer	BigInt	i64
Monetary Types	 	 	 
money	currency amount	Money	Cents
Character Types	 	 	 
character varying(n), varchar(n)	variable-length character string	Text	String, &str
character(n), char(n)	fixed-length character string	Text	String, &str
text	variable-length character string	Text	String, &str
Binary Data Types	 	 	 
bytea	binary data (“byte array”)	Binary	Vec<u8>, &u8
Date/Time Types	 	 	 
timestamp, timestamp(p) without time zone	date and time of day	Timestamp	chrono::NaiveDateTime
timestamptz, timestamp(p) with time zone	date and time of day, with time zone	Timestamptz	chrono::DateTime
date	calendar date (year, month, day)	Date	chrono::NaiveDate
time, time(p) without time zone	time of day (no date)	Time	chrono::NaiveTime
timetz, time(p) with time zone	time of day (no date), with time zone	 	 
interval(fields)(p)	time span	Interval	PgInterval
Boolean Type	 	 	 
boolean, bool	logical Boolean (true/false)	Bool	bool
Geometric Types	 	 	 
point (x,y)	geometric point on a plane	 	 
line {A,B,C}	infinite line on a plane	 	 
lseg ((x1,y1),(x2,y2))	finite line segment on a plane	 	 
box ((x1,y1),(x2,y2))	rectangular box on a plane	 	 
path ((x1,y1),...)	closed geometric path on a plane	 	 
path [(x1,y1),...]	open geometric path on a plane	 	 
polygon ((x1,y1),...)	closed geometric path on a plane	 	 
circle <(x,y),r\>	circle on a plane	 	 
Network Address Types	 	 	 
cidr	IPv4 or IPv6 network address	Cidr	ipnetwork::IpNetwork
inet	IPv4 or IPv6 host address	Inet	ipnetwork::IpNetwork
macaddr	MAC address	MacAddr	[u8; 6]
macaddr8	MAC address (EUI-64 format)	 	 
Enumerated Types	 	 	 
enum	enumerated value	(user-defined)	String, enum
Bit String Types	 	 	 
bit(n)	fixed-length bit string	 	 
bit varying(n), varbit	variable-length bit string	 	 
Text Search Types	 	 	 
tsvector	text search document	TsVector	 
tsquery	text search query	TsQuery	 
UUID Type	 	 	 
uuid	universally unique identifier	Uuid	uuid::Uuid
XML Type	 	 	 
xml	XML data	 	 
JSON Types	 	 	 
json	textual JSON data	Json	serde_json::Value
jsonb	binary JSON data, decomposed	Jsonb	serde_json::Value
Arrays	 	 	 
t[]	array of values	Array<T>	Vec<T>, Vec<Option<T>>, &[T], &[Option<T>]
Range Types	 	 	 
int4range	range of integer	Range<Integer>	(Bound<i32>, Bound<i32>)
int8range	range of bigint	Range<BigInt>	(Bound<i64>, Bound<i64>)
numrange	range of numeric	Range<Numeric>	(Bound<bigdecimal::BigDecimal>, Bound<bigdecimal::BigDecimal>)
tsrange	range of timestamp	Range<Timestamp>	(Bound<chrono::NaiveDateTime>, Bound<chrono::NaiveDateTime>)
tstzrange	range of timestamptz	Range<Timestamptz>	(Bound<chrono::DateTime>, Bound<chrono::DateTime>)
daterange	range of date	Range<Date>	(Bound<chrono::NaiveDate>, Bound<chrono::NaiveDate>)