Trouble getting set-returning-function to work #3015
-
I've got a plpgsql function I've tried sql_query("select * from create_user(?, ?);")
.bind::<diesel::sql_types::Text, _>(body.name)
.bind::<diesel::sql_types::Text, _>(private_id.to_string())
.first(conn) this will not compile, because of E0275 and sql_function! {
fn create_user(name: Text, password: Text) -> schema::users::SqlType;
}
let register_fn = create_user(body.name, private_id.to_string());
select(register_fn).first::<models::User>(conn) which compiles, but does not work (the Serialization into a Full snippet: let private_id = uuid::Uuid::new_v4();
let result = conn
.interact(move |conn| {
sql_query("select * from create_user(?, ?);")
.bind::<diesel::sql_types::Text, _>(body.name) // body.name is a String
.bind::<diesel::sql_types::Text, _>(private_id.to_string())
.first(conn)
// let register_fn = functions::create_user(body.name, private_id.to_string());
// select(register_fn).first::<models::User>(conn)
})
.await; And the compile error:
use chrono::{DateTime, Utc};
use serde::Serialize;
#[derive(Debug, Serialize, Queryable)]
pub struct User {
pub public_id: uuid::Uuid,
#[serde(skip_serializing)]
pub password_hashed: String,
pub name: String,
pub wins: i16,
pub losses: i16,
pub last_login: DateTime<Utc>,
}
table! {
users (public_id) {
public_id -> Uuid,
password_hashed -> Text,
name -> Text,
wins -> Int2,
losses -> Int2,
last_login -> Timestamptz,
}
}
create extension pgcrypto;
create table users (
public_id uuid primary key default gen_random_uuid(),
password_hashed text not null,
name text not null default 'Sportsfreund',
wins smallint not null default 0,
losses smallint not null default 0,
last_login timestamptz not null default now()
);
create function create_user(in name text, in password text, out users) as $$
begin
insert into users (name, password_hashed)
values (name, crypt(password, gen_salt('bf')))
returning * into $3;
end;
$$ language plpgsql security definer set search_path = public, pg_temp;
[package]
# ...
edition = 2021
[dependencies]
deadpool-diesel = {version = "0.3.1", features = ["rt_tokio_1", "postgres", "serde"]}
diesel = {version = "1.4.8", features = ["postgres", "uuidv07", "chrono"]}
chrono = {version = "0.4", features = ["serde"]}
uuid = {version = "0.8", features = ["serde", "v4"]}
serde = {version = "1", features = ["derive"]}
# ... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Please include the following information:
Otherwise any answer can only guess what might be wrong. |
Beta Was this translation helpful? Give feedback.
-
This fails because
Unfortunately does sql_function currently not support sql functions that can be used in sql_function! {
#[sql_name = "* FROM create_user"]
fn create_user(name: Text, password: Text) -> users::SqlType;
} you can add the missing from clause. Please note that this may result in future breakage as this is just a hacky workaround. |
Beta Was this translation helpful? Give feedback.
This fails because
.first()
expects a query that implementsLimitDsl
, so that it is able to apply an limit clause. That's not implemented for raw sql queries as applying an limit clause there may be not supported. Use.get_result()
in this case instead.Unfortunately does sql_function currently not …