-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sqlx::query!(..) inferring wrong Option
NULLable-types
#1852
Comments
I have a similar error with simpler setup. When using the macro (
My database schema: CREATE TABLE tblmedia(
id INTEGER PRIMARY KEY NOT NULL,
title VARCHAR NOT NULL,
raiting INTEGER,
file_name VARCHAR NOT NULL,
media_type TEXT NOT NULL
);
CREATE TABLE tblbook(
id INTEGER PRIMARY KEY NOT NULL,
author VARCHAR,
FOREIGN KEY (id) REFERENCES tblmedia(id) ON DELETE CASCADE
);
CREATE VIEW book AS
SELECT
m.id as id,
m.title as title,
b.author as author,
m.raiting as raiting,
m.file_name as file_name
FROM tblbook b
LEFT JOIN tblmedia m ON m.id = b.id; The struct: #[derive(Debug, PartialEq, Eq, sqlx::FromRow)]
pub struct Book {
pub id: i64,
pub author: Option<String>,
pub title: String,
pub raiting: Option<i64>,
pub file_name: String,
} The SQL queries // Does work
sqlx::query_as(r#"SELECT id, author, title, raiting, file_name FROM book;"#)
.fetch_all(connection)
.await;
// Does not work
sqlx::query_as!(
Book,
r#"SELECT id, author, title, raiting, file_name FROM book"#
)
.fetch_all(connection)
.await; The error
|
@abonander , when I execute [
{
"Plan": {
"Node Type": "Hash Join",
"Parallel Aware": false,
"Async Capable": false,
"Join Type": "Left",
"Startup Cost": 23.73,
"Total Cost": 49.77,
"Plan Rows": 1270,
"Plan Width": 104,
"Output": ["m.id", "b.author", "m.title", "m.raiting", "m.file_name"],
"Inner Unique": true,
"Hash Cond": "(b.id = m.id)",
"Plans": [
{
"Node Type": "Seq Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "tblbook",
"Schema": "public",
"Alias": "b",
"Startup Cost": 0.00,
"Total Cost": 22.70,
"Plan Rows": 1270,
"Plan Width": 36,
"Output": ["b.id", "b.author"]
},
{
"Node Type": "Hash",
"Parent Relationship": "Inner",
"Parallel Aware": false,
"Async Capable": false,
"Startup Cost": 16.10,
"Total Cost": 16.10,
"Plan Rows": 610,
"Plan Width": 72,
"Output": ["m.id", "m.title", "m.raiting", "m.file_name"],
"Plans": [
{
"Node Type": "Seq Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "tblmedia",
"Schema": "public",
"Alias": "m",
"Startup Cost": 0.00,
"Total Cost": 16.10,
"Plan Rows": 610,
"Plan Width": 72,
"Output": ["m.id", "m.title", "m.raiting", "m.file_name"]
}
]
}
]
}
}
] |
That's actually not a false positive, look back at the view you created: CREATE VIEW book AS
SELECT
m.id as id,
m.title as title,
b.author as author,
m.raiting as raiting,
m.file_name as file_name
FROM tblbook b
LEFT JOIN tblmedia m ON m.id = b.id; Most of those columns are coming from the Since you're selecting from |
Ahh. Thank you for pointing out my mistake. And I'm sorry for bothering you with this false positive. |
Running into something similar:
For the query I have:
And the struct:
And the error:
|
I had this same problem. I ended up resolving it by explicitly marking the joined-in column as optional |
This was very helpful and worked for me, thanks. My question is, is this SQL, Postgres, or SQLX syntax? Is this documented anywhere? This is very useful but there's no way I would have ever figured that out if I hadn't seen your comment. |
@Sharpiro I think you meant to @ someone else. However, I believe this issue is resolved in sqlx 0.7. That has been my experience in any case. Try upgrading and see if that helps |
Ahh sorry I meant to tag @martijnarts But I'm not really saying there is a problem, I'm just curious about the |
@Sharpiro this is SQLx behavior, and it's documented here. |
Great thanks |
I feel sorry for opening an issue and then not actively participating for a long while. For what I understood sqlx forwards SQL to the specific db server and get back the meta data for the query, which is a genius idea. If there are Thanks for clarifying. |
How does it work with enums? I have |
Hi , look at my new example, i don't use any use |
Environment:
sqlx = { version = "0.5", features = [ "runtime-async-std-native-tls", "postgres", "offline", "time" ] }
I don't think this issue is related to OS-specifics, so please let me know, if you need more information here.
Description
My DB schema:
I need to make a query with a subselect and some joins, where (in my opinion) sqlx has a bug.
At first here is a query which is smaller, but part of the larger query, which doesn't work.
This smaller one here works and also has a left join and subselect. sqlx correctly understands, that both select fields may be NULL, so great.
Now when I make the actual query it fails:
It's almost the same context: field of a left-joined table (which should always be consider nullable I guess?). And indeed
.await
returnsErr(..)
with message"error occurred while decoding column 4: unexpected null; try decoding as an
Option"
.Checking generated meta data, sqlx doesn't get the
nullable
fields correctly.sqlx-data.json
:Is it possible to have a workaround maybe without offline queries in this case, but still use the feature for the rest of the project?
Thank you
Philipp
The text was updated successfully, but these errors were encountered: