@@ -1884,7 +1884,19 @@ impl<'a> QueryFragment<Pg> for FindRangeQuery<'a> {
1884
1884
} else {
1885
1885
self . mut_range . compare_column ( & mut out)
1886
1886
}
1887
- out. push_sql ( "as block_number, id, vid\n " ) ;
1887
+ // Cast id to bytea to ensure consistent types across UNION
1888
+ // The actual id type can be text, bytea, or numeric depending on the entity
1889
+ out. push_sql ( "as block_number, " ) ;
1890
+ let pk_column = table. primary_key ( ) ;
1891
+
1892
+ // We only support entity id types of string, bytes, and int8.
1893
+ match pk_column. column_type {
1894
+ ColumnType :: String => out. push_sql ( "id::bytea" ) ,
1895
+ ColumnType :: Bytes => out. push_sql ( "id" ) ,
1896
+ ColumnType :: Int8 => out. push_sql ( "id::text::bytea" ) ,
1897
+ _ => out. push_sql ( "id::bytea" ) ,
1898
+ }
1899
+ out. push_sql ( " as id, vid\n " ) ;
1888
1900
out. push_sql ( " from " ) ;
1889
1901
out. push_sql ( table. qualified_name . as_str ( ) ) ;
1890
1902
out. push_sql ( " e\n where" ) ;
@@ -1906,7 +1918,7 @@ impl<'a> QueryFragment<Pg> for FindRangeQuery<'a> {
1906
1918
// In case we have only immutable entities, the upper range will not create any
1907
1919
// select statement. So here we have to generate an SQL statement thet returns
1908
1920
// empty result.
1909
- out. push_sql ( "select 'dummy_entity' as entity, to_jsonb(1) as data, 1 as block_number, 1 as id, 1 as vid where false" ) ;
1921
+ out. push_sql ( "select 'dummy_entity' as entity, to_jsonb(1) as data, 1 as block_number, ' \\ x'::bytea as id, 1 as vid where false" ) ;
1910
1922
} else {
1911
1923
out. push_sql ( "\n order by block_number, entity, id" ) ;
1912
1924
}
0 commit comments