Support for "CREATE DOMAIN" types? #3871
-
I'm experimenting with a learning project, and I'm a bit confused. It looks like SQLx supports PostgreSQL's As a toy example, let's use this schema: CREATE DOMAIN "hex_16" AS varchar(16)
CHECK ("value" ~ '^[0-9A-F]{16}$');
CREATE TABLE IF NOT EXISTS "twag_thingies" (
"id" hex_16 UNIQUE NOT NULL PRIMARY KEY,
"target_url" text NOT NULL,
"access_count" integer DEFAULT 0
); Then a Rust-side domain model, #[derive(sqlx::Type, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[sqlx(type_name = "hex_16", transparent)]
pub struct Hex16(String);
impl Hex16 {
pub fn new(s: impl Into<String>) -> Result<Self, &'static str> { Ok(Hex16(s.into().to_uppercase())) }
}
#[derive(sqlx::FromRow)]
pub struct Thingie {
pub id: Hex16,
pub target_url: String,
pub access_count: i32,
} If I try and use
I've tried a few variations on this, without much luck. Can someone help me understand how to map into/from Postgres-side newtypes and Rust-side newtypes, retaining type-safety as much as possible? (I'm also new to Rust, which potentially isn't helping; but I'm trying to make a learning project out of this. I'm primarily familiar with OCaml, TypeScript, Swift, et al, if that's relevant.) Edit: I see #3856 which actually clarifies the exact error I'm getting (thanks, @dyc3!); so I think I've found the correct part of the documentation, at least. "Type Overrides: Bind Parameters (Postgres only)" says:
However, when I try to use
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
See the example in the section you linked. The |
Beta Was this translation helpful? Give feedback.
See the example in the section you linked. The
as
goes in the bind expression, not the SQL itself.