Comments (3)
The one thing that is different is that we are using diesel-async, could that be the problem?
If that's the problem you are in the wrong issue tracker here, as diesel-async
is a different project. If you open an issue there please try to remember providing a complete example.
from diesel.
Thanks for opening this bug report.
I've tried the following code:
use std::io::Write;
use diesel::connection::SimpleConnection;
use diesel::deserialize;
use diesel::deserialize::FromSql;
use diesel::deserialize::FromSqlRow;
use diesel::expression::AsExpression;
use diesel::pg::Pg;
use diesel::pg::PgValue;
use diesel::prelude::*;
use diesel::serialize;
use diesel::serialize::IsNull;
use diesel::serialize::Output;
use diesel::serialize::ToSql;
pub mod postgres {
pub mod schema {
pub mod sql_types {
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "action"))]
pub struct Action;
}
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::Action;
directory_actions (id) {
id -> Int4,
credentials_id -> Int4,
#[max_length = 255]
directory_path -> Varchar,
actions -> Array<Nullable<Action>>,
}
}
}
}
#[derive(Debug, FromSqlRow, AsExpression, Clone)]
#[diesel(sql_type = crate::postgres::schema::sql_types::Action)]
pub enum Action {
Read,
Write,
Delete,
List,
Admin,
}
// Implement ToSql for the custom enum
impl ToSql<crate::postgres::schema::sql_types::Action, Pg> for Action {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
match *self {
Action::Read => out.write_all(b"read")?,
Action::Write => out.write_all(b"write")?,
Action::Delete => out.write_all(b"delete")?,
Action::List => out.write_all(b"list")?,
Action::Admin => out.write_all(b"admin")?,
}
Ok(IsNull::No)
}
}
// Implement FromSql for the custom enum
impl FromSql<crate::postgres::schema::sql_types::Action, Pg> for Action {
fn from_sql(bytes: PgValue) -> deserialize::Result<Self> {
match bytes.as_bytes() {
b"read" => Ok(Action::Read),
b"write" => Ok(Action::Write),
b"delete" => Ok(Action::Delete),
b"list" => Ok(Action::List),
b"admin" => Ok(Action::Admin),
_ => Err("Unrecognized enum variant".into()),
}
}
}
#[derive(Debug, Identifiable, Queryable, Selectable, Insertable)]
#[diesel(table_name = crate::postgres::schema::directory_actions)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct DirectoryActions {
pub id: i32,
pub credentials_id: i32,
pub directory_path: String,
pub actions: Vec<Option<Action>>,
}
fn main() {
let mut conn = PgConnection::establish("postgres://localhost/diesel_test").unwrap();
conn.begin_test_transaction().unwrap();
conn.batch_execute("CREATE TYPE Action AS ENUM ('read', 'write', 'delete', 'list', 'admin');
CREATE TABLE
directory_actions (
id SERIAL PRIMARY KEY,
credentials_id INTEGER NOT NULL,
directory_path VARCHAR(255) NOT NULL,
actions Action[] NOT NULL check (actions <> '{}' and array_position(actions, null) is null)
);").unwrap();
let directory_actions = vec![DirectoryActions {
id: 1,
credentials_id: 1,
directory_path: "".into(),
actions: vec![Some(Action::Read)],
}];
diesel::insert_into(crate::postgres::schema::directory_actions::table)
.values(&directory_actions)
.execute(&mut conn)
.unwrap();
let res = crate::postgres::schema::directory_actions::table
.select(DirectoryActions::as_select())
.load(&mut conn)
.unwrap();
dbg!(res);
}
This runs without issues for me. I'm closing this issue as cannot reproduce. If you still hit this bug, feel free to provide a minimal self contained reproducible example (something someone can just do git clone … && cargo run
).
from diesel.
Thanks for you swift reply, your example indeed seems to work. So something is wrong in my project and I will try to figure out what. The one thing that is different is that we are using diesel-async
, could that be the problem?
from diesel.
Related Issues (20)
- cargo test fails with a lot of errors. HOT 1
- Error mapping one column type to an entire struct. HOT 3
- fail to compile `diesel = "2.1.1"`
- Regression in distinct_on: cannot use .nullable() on column
- Compilation error when grouping aliased fields
- Compilation fails when attempting to create a nested join from an aliased table.
- Error: cannot find attribute ` max_ Length ` in this scope HOT 1
- error: expected one of `{`, lifetime, or type, found `:` HOT 2
- Documentation: Improve documentation for upsert `.filter_target()` HOT 2
- splitting into multiple schema.rs HOT 1
- Interaction between `#[derive(AsChangeset)]` and macro in type position HOT 2
- SQLite + PRAGMA foreign_keys = ON => missing error HOT 9
- `.distinct_on` is limited to maximum three `.order_by`s HOT 2
- Runtime Error for count() with order_by clause combined HOT 5
- fix: `sql_query` error "`Untyped: CompatibleType<std::string::String, Pg>` is not satisfied" HOT 1
- Impossible to combine into_boxed and group_by
- Impossible to combine into_boxed and group_by
- Nullable<diesel::sqlite::sql_types::Timestamptz> throws trait not implement errors
- Segmentation Fault in R2D2 postgres (interrupted by signal 11: SIGSEGV) HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from diesel.