drift icon indicating copy to clipboard operation
drift copied to clipboard

Generate nullable field from subqery

Open minia68 opened this issue 4 years ago • 6 comments

Hi, with this kind of query in .moor file: queryWithSubqeury: select t.column1, (select column2 from table2 where table1_id = t.id limit 1) table2_column2 from table1 t, moor generate result class with non nullable field for table2_column2(from subqery). If table2 have 0 rows, table2_column2 will be null and i got type cast error.

minia68 avatar May 29 '21 14:05 minia68

Good catch, thanks. I've considered just making everything coming from a subquery nullable, but I'm afraid that may break existing code (especially code using COUNT(*) in a subquery where a non-nullable type would be more appropriate).

In the meantime, you could either use NULLIF(column2, NULL) which is a no-op and convinces moor that a column is nullable. Alternatively, you could convert the subquery to a join which should get analyzed correctly:

select t.column1, t2.column2
  from table1 t
  left outer join (select * from table2 where table1_id  = t.id limit 1) t2

simolus3 avatar May 29 '21 15:05 simolus3

Hello!

With moor_generator 4.3.1, and following schema and query:

CREATE TABLE inboxes (
    id TEXT PRIMARY KEY NOT NULL,
    group_id TEXT NOT NULL
);

CREATE TABLE assignable_users (
    user_id TEXT NOT NULL,
    inbox_id TEXT NOT NULL
);

inboxesForGroup:    SELECT *
                    FROM inboxes as i
                    LEFT JOIN assignable_users as au
                    ON au.inbox_id = i.id
                    WHERE group_id = :group_id;

The class generated for the query declares userId to be a non-nullable String. I'm pretty sure it should nullable.

matehat avatar Jun 02 '21 18:06 matehat

@matehat Very good catch, thanks! I've fixed this in 5d7cb7dbba58ea6e235275ebf09614d46994859d.

simolus3 avatar Jun 03 '21 11:06 simolus3

Awesome, thanks!

matehat avatar Jun 03 '21 12:06 matehat

do you have any ETA for releasing these fixes?

knaeckeKami avatar Jun 29 '21 20:06 knaeckeKami

I'll try to release a new moor version this weekend. The fixes required pretty substantial changes to how queries are analyzed, so I couldn't push a small hotfix release.

simolus3 avatar Jun 30 '21 08:06 simolus3