Nullable arguments in .drift
file queries
#2493
-
Is it possible to have a nullable argument in a query that I have defined in a For instance, when querying persons, I always pass a first name. If I pass a CREATE TABLE persons (
id INT NOT NULL PRIMARY KEY,
first_name TEXT NULL,
last_name TEXT NULL
);
getPersons: SELECT * FROM persons
WHERE first_name = :firstName AND (:lastName IS NULL OR last_name = :lastName); When building the above, both I guess I could hack my way out of this by passing and checking for an empty string: getPersonsEmptyString: SELECT * FROM persons
WHERE first_name = :firstName AND (:lastName = '' OR last_name = :lastName); Or by something like the following: getPersonsEqualSelf: SELECT * FROM persons
WHERE first_name = :firstName AND last_name= IFNULL(:lastName, last_name); (Though come to think of it, I'm not sure what the last query would do if |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
In cases where type inference fails, you can also declare the types of variables explicitly: getPersons(:lastName AS TEXT OR NULL): SELECT * FROM persons
WHERE first_name = :firstName AND (:lastName IS NULL OR last_name = :lastName); But I'm surprised that drift generates non-nullable parameters here since both columns are nullable to begin with. I couldn't reproduce this, are you on a recent drift version? |
Beta Was this translation helpful? Give feedback.
-
Hi, I have a related but different scenario:
When I pass null for parentId, I want to check explicitly the parentId is null, |
Beta Was this translation helpful? Give feedback.
In cases where type inference fails, you can also declare the types of variables explicitly:
But I'm surprised that drift generates non-nullable parameters here since both columns are nullable to begin with. I couldn't reproduce this, are you on a recent drift version?