Skip to content

Commit 3356023

Browse files
committed
Try to insert non-numberic value without default value.
1 parent 2495b40 commit 3356023

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

examples/proxy_gluesql_example/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ publish = false
88
[workspace]
99

1010
[dependencies]
11+
anyhow = "^1"
1112
async-std = { version = "1.12", features = ["attributes", "tokio1"] }
1213
serde_json = { version = "1" }
1314
serde = { version = "1" }
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use sea_orm::entity::prelude::*;
1+
use sea_orm::{entity::prelude::*, ActiveValue::*};
22
use serde::{Deserialize, Serialize};
33

44
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
55
#[sea_orm(table_name = "posts")]
66
pub struct Model {
77
#[sea_orm(primary_key)]
8-
pub id: i64,
8+
pub id: Uuid,
99

1010
pub title: String,
1111
pub text: String,
@@ -14,4 +14,15 @@ pub struct Model {
1414
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
1515
pub enum Relation {}
1616

17-
impl ActiveModelBehavior for ActiveModel {}
17+
#[async_trait::async_trait]
18+
impl ActiveModelBehavior for ActiveModel {
19+
async fn before_save<C>(self, _db: &C, _insert: bool) -> Result<Self, DbErr>
20+
where
21+
C: ConnectionTrait,
22+
{
23+
println!("Before save");
24+
let mut ret = self.clone();
25+
ret.id = Set(Uuid::new_v4());
26+
Ok(ret)
27+
}
28+
}

examples/proxy_gluesql_example/src/main.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
mod entity;
66

7+
use anyhow::{anyhow, Result};
78
use std::{
89
collections::BTreeMap,
910
sync::{Arc, Mutex},
1011
};
1112

1213
use gluesql::{memory_storage::MemoryStorage, prelude::Glue};
1314
use sea_orm::{
14-
ActiveValue::Set, Database, DbBackend, DbErr, EntityTrait, ProxyDatabaseTrait, ProxyExecResult,
15-
ProxyRow, Statement,
15+
ActiveModelTrait, ActiveValue::Set, Database, DbBackend, DbErr, EntityTrait,
16+
ProxyDatabaseTrait, ProxyExecResult, ProxyRow, Statement,
1617
};
1718

1819
use entity::post::{ActiveModel, Entity};
@@ -99,6 +100,12 @@ impl ProxyDatabaseTrait for ProxyDb {
99100
val.unwrap_or(0).to_string(),
100101
false,
101102
),
103+
sea_orm::Value::Uuid(val) => {
104+
Value::SingleQuotedString(match val {
105+
Some(val) => val.to_string(),
106+
None => "".to_string(),
107+
})
108+
}
102109
_ => todo!(),
103110
};
104111
}
@@ -132,14 +139,14 @@ impl ProxyDatabaseTrait for ProxyDb {
132139
}
133140

134141
#[async_std::main]
135-
async fn main() {
142+
async fn main() -> Result<()> {
136143
let mem = MemoryStorage::default();
137144
let mut glue = Glue::new(mem);
138145

139146
glue.execute(
140147
r#"
141148
CREATE TABLE IF NOT EXISTS posts (
142-
id INTEGER PRIMARY KEY,
149+
id UUID PRIMARY KEY,
143150
title TEXT NOT NULL,
144151
text TEXT NOT NULL
145152
)
@@ -160,32 +167,41 @@ async fn main() {
160167
println!("Initialized");
161168

162169
let data = ActiveModel {
163-
id: Set(11),
164170
title: Set("Homo".to_owned()),
165171
text: Set("いいよ、来いよ".to_owned()),
172+
..Default::default()
166173
};
167-
Entity::insert(data).exec(&db).await.unwrap();
174+
175+
println!("data: {:?}", data);
176+
let ret = data.insert(&db).await.map_err(|err| anyhow!("{:?}", err))?;
177+
println!("ret: {:?}", ret);
178+
168179
let data = ActiveModel {
169-
id: Set(45),
170180
title: Set("Homo".to_owned()),
171181
text: Set("そうだよ".to_owned()),
182+
..Default::default()
172183
};
173-
Entity::insert(data).exec(&db).await.unwrap();
184+
let ret = data.insert(&db).await.map_err(|err| anyhow!("{:?}", err))?;
185+
println!("ret: {:?}", ret);
186+
174187
let data = ActiveModel {
175-
id: Set(14),
176188
title: Set("Homo".to_owned()),
177189
text: Set("悔い改めて".to_owned()),
190+
..Default::default()
178191
};
179-
Entity::insert(data).exec(&db).await.unwrap();
192+
let ret = data.insert(&db).await.map_err(|err| anyhow!("{:?}", err))?;
193+
println!("ret: {:?}", ret);
180194

181195
let list = Entity::find().all(&db).await.unwrap().to_vec();
182196
println!("Result: {:?}", list);
197+
198+
Ok(())
183199
}
184200

185201
#[cfg(test)]
186202
mod tests {
187203
#[smol_potat::test]
188204
async fn try_run() {
189-
crate::main()
205+
crate::main().unwrap();
190206
}
191207
}

src/executor/insert.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,9 @@ where
256256
if db_backend == DbBackend::MySql && last_insert_id == 0 {
257257
return Err(DbErr::RecordNotInserted);
258258
}
259-
Some(
260-
ValueTypeOf::<A>::try_from_u64(last_insert_id)
261-
.map_err(|_| DbErr::UnpackInsertId)?,
262-
)
259+
ValueTypeOf::<A>::try_from_u64(last_insert_id)
260+
.map(|val| Some(val))
261+
.unwrap_or(None)
263262
} else {
264263
None
265264
}

0 commit comments

Comments
 (0)