simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.43k stars 351 forks source link

postgresql arrays? #2996

Closed Noxits closed 1 month ago

Noxits commented 1 month ago

Is your feature request related to a problem? Please describe. Are postgresql arrays supported? I've stumbled accros the issues/disscusions and code but couldn't find anything. Or is there a dark magic way with custom queries?

Describe the solution you'd like Support for postgresql arrays

simolus3 commented 1 month ago

I think the postgres package only supports some array types, but we should definitely have support for those in drift_postgres.

Or is there a dark magic way with custom queries?

You can use custom SQL types as a workaround until we have official support for them. That's not even dark magic, an official API in package:drift_postgres will be built with the same thing :)

simolus3 commented 1 month ago

I've added them to drift_postgres in c621ef9c6be3ef83a948124fcc830517b22b5559.

Noxits commented 1 month ago

Sweet, thanks :D

You can use custom SQL types as a workaround

Oopsies, totally forgot about this functionality, my bad

Noxits commented 1 month ago

Hmm, it seems I can get it working with default values

class User extends Table {
    Column<List<int>> get example => customType(PgTypes.bigIntArray).withDefault(const Constant([]))();
}

Throws :

Invalid argument (dart): Must be null, bool, String, int, DateTime, Uint8List or double: _ImmutableList len:0
simolus3 commented 1 month ago

Ah right, I think you'd have that problem with all custom types - at the moment we're unfortunately unable to infer the type to apply in that context.

Does it work if you explicitly apply the type on the constant, e.g Constant([], PgTypes.bigIntArray)?

Noxits commented 1 month ago

It indeed does, thank you very much.

Sorry, I should really open my eyes somehow, the solution was right in front of me..

simolus3 commented 1 month ago

No problem - the fact that the custom type has to be specified twice here not intuitive at all, I consider this to be a flaw in drift. It's just not easy to fix...