dart-backend / angel

A polished, production-ready backend framework in Dart for the VM, AOT, and Flutter.
https://github.com/dukefirehawk/angel
BSD 3-Clause "New" or "Revised" License
172 stars 22 forks source link

Column type "text" not working to generate a functioning model.g.dart file for connecting to MariaDB. #152

Open itsnotmeman opened 1 month ago

itsnotmeman commented 1 month ago

I have this orm model:

@Orm(tableName: 'database_table', generateMigrations: false)
@serializable
abstract class _MyModel {

  @primaryKey
  @SerializableField(alias: 'RowId', isNullable: false)
  int get rowId;

  @Column(type: ColumnType.text)
  String? nameEn;  
}

Then I run dart run build_runner build to generate the model.g.dart file. This works.

But an error occurs when I try to run my program which looks like this:

Future<List<MyModel >> getTableData() async {
  var settings = ConnectionSettings(
      host: 'localhost',
      port: 3306,
      db: database,
      user: user,
      password: password);

  var connection = await MySqlConnection.connect(settings);
  var executor = MariaDbExecutor(connection);

  // Get all fields of the table.
  var query = MyModelQuery();
  var result = await query.get(executor);

  await connection.close();
  return result;
}

The exception message:

Exception has occurred.
_TypeError (type 'Blob' is not a subtype of type 'String?' in type cast)

The exception occurs in the model.g.dart file in the "parseRow" function.

  Optional<MyModel > parseRow(List row) {
    if (row.every((x) => x == null)) {
      return Optional.empty();
    }
    var model = MyModel (
      nameEn: fields.contains('name_en') ? (row[0] as String?) : null,  //<--- THE EXCEPTION THROWS HERE.
      rowId: fields.contains('RowId') ? (row[1] as int) : 0,
    );
    return Optional.of(model);
  }

Everything works fine if I replace the problematic line with:

nameEn: fields.contains('name_en') ? (row[0].toString()) : null,

If the field in the database is a VarChar and not a Text, everything runs correctly. So I think it's the "@Column(type: ColumnType.text)" which doesn't work as it supposed.

I had to create a class and override the "parseRow" function to make it work. Would be great if this issue could be fixed. Thank you.

dukefirehawk commented 4 weeks ago

Support for text data type in MySQL/MariaDB is on the to do list.