https://github.com/pickfire/diesel-upsert
diesel upsert experiment
https://github.com/pickfire/diesel-upsert
Last synced: 29 days ago
JSON representation
diesel upsert experiment
- Host: GitHub
- URL: https://github.com/pickfire/diesel-upsert
- Owner: pickfire
- Created: 2021-03-08T14:52:38.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-03-09T11:04:42.000Z (about 4 years ago)
- Last Synced: 2025-02-10T11:45:16.964Z (3 months ago)
- Language: Rust
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Diesel upsert experiment
========================Previous: https://github.com/pickfire/diesel-join-not-exist
Next: https://github.com/pickfire/diesel-group-byThis time it requires diesel-git which isn't released.
Rust not able to recommend `users::table` when `hello::schema::users` is used
even though it is a relative re-export.`on_conflict` gets runtime error instead of compile-time error when non
primary key or unique key is used. TODOI was wondering how upsert could be done but is still surprised that all these
could be made type-safe although there are a little bit of edge cases not being
covered.The example I tested out here have 3 struct (based on the previous ones).
sqlite was used for easy testing.+---------+ +---------+ +---------+
| User |<-+ | Post | | Comment |
+---------+ | +---------+ +---------+
| id | | | id | | id |
| name | | | title | | body |
| | | | body |<----+ post_id |
| | | | user_id +--+--+ user_id |
+---------+ | +---------+ | +---------+
+---------------+I want to find all posts that does not have any comments. The query,
```rust
let query = diesel::insert_into(users)
.values(&user)
.on_conflict(id)
.do_update()
.set(name.eq(excluded(name)));
```Which results in the SQL query,
```sql
INSERT INTO `users` (`id`, `name`)
VALUES (?, ?)
ON CONFLICT (`id`)
DO UPDATE SET `name` = excluded.`name` -- binds: [2, "John"]
```## Get started
Rust, diesel_cli (with `sqlite` feature) is required.
```
$ diesel migration run
$ cargo run --bin init # populate database
$ cargo run --bin hello # multi join query
```