Closed BarneyKY closed 3 days ago
For example: Postgres:
CREATE TABLE "public"."goods" (
"code" varchar(8) COLLATE "pg_catalog"."default" NOT NULL,
"is_onsale" int2 NOT NULL,
PRIMARY KEY ("code")
);
CREATE VIEW "public"."goods_code" AS
SELECT
goods.code AS code
FROM
goods
WHERE
( goods.is_onsale = 1 )
ORDER BY
goods.code;
When i use SQL directly
When i use VIEW
so strange
https://docs.rs/sqlx/latest/sqlx/macro.query.html#nullability-output-columns
finally, according to the docs, i find out the reason
I wanna know why do like that, it makes me don't want use the VIEW but only the directly SQL
You can also enforce whether to use Option values or not by selecting each column individually:
let products = sqlx::query_as!(
ProductFull,
r#"
SELECT
p.id as "id!",
p.name as "name!",
p.description as "description",
p.price as "price!",
p.image as "image",
p.created_at as "created_at!",
p.sell_count as "sell_count!"
FROM public.product_with_sells p
"#,
)
Add an exclamation mark to enforce NOT NULL.
You can also enforce whether to use Option values or not by selecting each column individually:
let products = sqlx::query_as!( ProductFull, r#" SELECT p.id as "id!", p.name as "name!", p.description as "description", p.price as "price!", p.image as "image", p.created_at as "created_at!", p.sell_count as "sell_count!" FROM public.product_with_sells p "#, )
Add an exclamation mark to enforce NOT NULL.
Thanks for your suggestion, it is the right way to solve this problem!
Bug Description
I use sqlx prepare to check the database and the result is like this:
And i wrote this in Rust
goods_storage
is aVIEW
in postgre, actually most of the column areNOT NULL
, when i use the view's sql directly, they are not in Option<>, but when i use view to query, all columns are nullable like thisI just don't want check this value whether it is some or none and i ensure that they are not null ( especially there is a primary key cloumn, and not in view, SQL directly works well )
How can i do ?
Info
rustc --version
: rustc 1.79.0 (129f3b996 2024-06-10)