schultek / stormberry

Access your postgres database effortlessly from dart code.
https://pub.dev/packages/stormberry
MIT License
68 stars 17 forks source link

FormatException: Invalid type code in substitution variable '@2:bool' #52

Closed mhmzdev closed 1 year ago

mhmzdev commented 1 year ago

stormberry: 0.12.1

The error because of dart type bool and postgres SQL type boolean. As I'm using bool in my dart classes, so it's failing to bind that class or convert it into a SQL based query.

The moment I modify the model.schema.g file and replace all bool with boolean it start working.

Model Class

@Model()
abstract class Post {
  @PrimaryKey()
  @AutoIncrement()
  int get id;

  int get uid;
  String get caption;
  bool? get hasImage;
  String? get imageUrl;
  bool? get hasVideo;
  String? get videoUrl;
  List<int> get likes;
  List<int> get comments;

  DateTime get createdAt;
}

Schema Class

This is where I make change to fix this, replace bool with boolean and it will work.

  @override
  Future<List<int>> insert(List<PostInsertRequest> requests) async {
    if (requests.isEmpty) return [];
    var values = QueryValues();
    var rows = await db.query(
      'INSERT INTO "posts" ( "uid", "caption", "has_image", "image_url", "has_video", "video_url", "likes", "comments", "created_at" )\n'
      'VALUES ${requests.map((r) => '( ${values.add(r.uid)}:int8, ${values.add(r.caption)}:text, ${values.add(r.hasImage)}:bool, ${values.add(r.imageUrl)}:text, ${values.add(r.hasVideo)}:bool, ${values.add(r.videoUrl)}:text, ${values.add(r.likes)}:_int8, ${values.add(r.comments)}:_int8, ${values.add(r.createdAt)}:timestamp )').join(', ')}\n'
      'RETURNING "id"',
      values.values,
    );
    var result = rows
        .map<int>((r) => TextEncoder.i.decode(r.toColumnMap()['id']))
        .toList();

    return result;
  }

‼️ Error

ERROR - 2023-03-02 10:06:38.503874
POST /api/v1/posts/
Error thrown by handler.
FormatException: Invalid type code in substitution variable '@2:bool'
package:postgres/src/query.dart 382:9            new PostgreSQLFormatIdentifier
package:postgres/src/substituter.dart 140:28     PostgreSQLFormat.substitute.<fn>
dart:_internal                                   ListIterable.join
package:postgres/src/substituter.dart 157:8      PostgreSQLFormat.substitute
package:postgres/src/query.dart 75:40            Query.sendExtended
package:postgres/src/connection_fsm.dart 375:9   _PostgreSQLConnectionStateReadyInTransaction.processQuery
package:postgres/src/connection_fsm.dart 361:14  _PostgreSQLConnectionStateReadyInTransaction.awake
package:postgres/src/connection.dart 620:67      _PostgreSQLExecutionContextMixin._enqueue
package:postgres/src/connection.dart 521:15      _PostgreSQLExecutionContextMixin._query
package:postgres/src/connection.dart 475:7       _PostgreSQLExecutionContextMixin.query
package:stormberry/src/core/database.dart 91:34  Database.query
schultek commented 1 year ago

Oh I thought it was also called bool in sql, but it appears to be boolean. I can just change that.

Its weird that there weren't any issues so far. I think its probably handled as an alias, mut maybe not in all cases.

Edit: Its actually not an issue with the database, but the 'postgres' dart package that does not handle 'bool' as an alias for 'boolean'. But I will change it anyways to be consistent with the spec.

mhmzdev commented 1 year ago

Thanks man Let me know if I need to close this issue

p.s are you maintaining postgres as well? Or just this stormberry?

schultek commented 1 year ago

I will close it when done.

No just this.

schultek commented 1 year ago

This is fixed now with v0.13.0.