drift icon indicating copy to clipboard operation
drift copied to clipboard

Postgres JSONB customInsert

Open jbbjarnason opened this issue 5 months ago • 1 comments

When using the package:postgres without drift I would have the following test pass:

      test('should handle complex JSON data', () async {
        final now = DateTime.now();
        final complexData = {
          'sensor_id': 'temp_001',
          'readings': {
            'temperature': 25.5,
            'humidity': 60.2,
            'pressure': 1013.25,
          },
          'metadata': {
            'location': 'room_a',
            'calibration_date': '2024-01-01',
            'active': true,
          },
          'alerts': ['high_temp', 'low_humidity'],
        };

        await database.insertTimeseriesData(testTableName, now, complexData);

        final result = await database.queryTimeseriesData(
            testTableName, now.subtract(const Duration(days: 1)));
        expect(result.length, 1);
        expect(result[0].value, complexData);
      });

Now I have changed to drift and the results of the test has the following statements(log)

Drift: Sent     SELECT EXISTS (
      SELECT 1
      FROM information_schema.tables
      WHERE table_schema = 'public'
        AND table_name = $1
    ) AS "exists"
     with args [test_timeseries]
Drift: Sent       CREATE TABLE IF NOT EXISTS "test_timeseries" (
        time TIMESTAMPTZ, sensor_id TEXT, readings JSONB, metadata JSONB, alerts TEXT[]
      )
     with args []
Drift: Sent       SELECT create_hypertable('"test_timeseries"', 'time', if_not_exists => TRUE, migrate_data => TRUE);
     with args []
Drift: Sent       SELECT remove_retention_policy('"test_timeseries"', if_exists => TRUE);
     with args []
Drift: Sent           SELECT add_retention_policy('"test_timeseries"', drop_after => INTERVAL '3600000000 microseconds');
         with args []
Drift: Sent INSERT INTO "test_timeseries" ("time", "sensor_id", "readings", "metadata", "alerts") VALUES ($1::timestamptz, $2, $3, $4, $5) with args [2025-07-20T06:51:15.456761 +00:00, temp_001, {temperature: 25.5, humidity: 60.2, pressure: 1013.25}, {location: room_a, calibration_date: 2024-01-01, 
active: true}, [high_temp, low_humidity]]

I get now the following exception,

Exception has occurred.
ArgumentError (Invalid argument (value): Unsupported type: _Map len:3)

In Drift's baseexecutor

Image

I would like to contribute, help fixing this, I am just not sure where to start.

jbbjarnason avatar Jul 20 '25 07:07 jbbjarnason

The debugger isn't particularly helpful here - the actual source of the exception is likely in the _PgDelegate class at package:drift_postgres/src/pg_database.dart, most likely in _runWithArgs.

Can you share the drift definition of that table? You'd probably have to use type converters to store JSON values, are you doing that?

simolus3 avatar Jul 20 '25 19:07 simolus3