drift icon indicating copy to clipboard operation
drift copied to clipboard

`GeneratedColumn` name cannot be one of the inherited Table methods when generated from Drift files

Open dballance opened this issue 3 years ago • 2 comments

You cannot name any database columns with the same name as any of the Table abstract class methods.

This means, for instance, that you cannot have a column of 'text', since it clashes with the Table helper methods used for Dart tables.

final TextColumn get text => text()

The above probably fails with Dart tables, but when using Drift files it's happily ignored until compile time.

Seems like those methods could be made static or moved into a separate table builder DSL.

Exception during the flutter compile:

: Error: Can't declare a member that conflicts with an inherited one.
late final GeneratedColumn<String> text = GeneratedColumn<String>(
                                     ^^^^
/opt/flutter/.pub-cache/hosted/pub.dartlang.org/drift-2.1.0/lib/src/dsl/table.dart:150:25: Context: This is the inherited member.
  ColumnBuilder<String> text() => _isGenerated();
                        ^^^^

Simple Drift File:

CREATE TABLE foo (
text     TEXT,
);

dballance avatar Oct 13 '22 18:10 dballance

I want to add I'm quite happy with Drift, especially with the updated SQLCipher support and Drift file integration. It's been great to work with so far.

This is the first edge I've hit - and wanted to at least report it.

A good workaround is simply changing the column name and doing any column renaming at the edge (for instance, parsing some API entity to the new column name when used in the app).

dballance avatar Oct 13 '22 18:10 dballance

Inside a .drift file, you can change the Dart getter name of a column like this:

CREATE TABLE foo (
  text     TEXT AS dartText,
);

This can be used as a workaround for this problem.

I think reducing the amount of members added into the default table class is a worthy goal though. For instance, the column builder methods could probably be an extension.

simolus3 avatar Oct 13 '22 20:10 simolus3