2

I want my table names to be specific as I am migrating from Native Android Room database to Flutter using Drift library for database.

Room take a table name i.e. class name and creates a table:

e.g. the following will create table 'User' with field names as 'firstName' and lastName' in native Android using Room database support

@Entity
data class User(
    @PrimaryKey val id: Int,

    val firstName: String?,
    val lastName: String?
)

When I try to replicate this in Flutter, I write:

    @DataClassName('User') // To avoid drift changing it to 'user'
    data class User(
        TextColumn get firstName => text().named('firstName')();
        TextColumn get lastName => text().named('lastName')();
// The above '.named()' makes sure drift does not change names to 'first_name' & 'last_name'
    )

By doing above I am making sure the databases match exactly! (please correct me if I am wrong here)

Now when I try to replicate the migration statements in drift I am writing:

...
  if (from < 4) await m.addColumn(Note, Note.path);
  if (from < 5) await m.addColumn(Profile, Profile.squarePicture);
...

As you can see that I am writing note as a 'Note' because I have defined my table name as 'Note' and not 'note' same goes with profile! But the code is highlighting the following error:

The argument type 'Type' can't be assigned to the parameter type 'TableInfo<Table, dynamic>'

When I change table names to small alphabets i.e. 'n' and 'p' the error goes away but I do not understand WHY? My table names are explicitly with capital letters in the start.

I do not want to change anything in the database when my users will upgrade their app which was previously made in Native Android using Kotlin and Room database to the Flutter version. I want them to have a seamless experience with no loss of data!

Damandroid
  • 756
  • 9
  • 31

1 Answers1

0

The @DataClassName() annotation is used to change the name of the class which is generated by the drift. This generated class is what the drift will use when querying the database and getting the result.

By default, the generated class name will be the same as the class name except the drift will strip any last 's' character. For example, if your class name is "Users" the drift will generate a class with the name "User" with the 's' at the end stripped.

Coming to your question @DataClassName() has nothing to do with the table name in your database or any migration strategy. If you want to change the table name you can do something like this,

class User extends Table {
    
    ///This property will change the table name.
    String get tableName => 'User';

    ...
}

Official Documentation - Dart table Names

Drift also generates a class that has an identical name to our table class. In your case, there will be a class generated $User which extends our table class, and a mixin TableInfo. This class's object is automatically created by the drift in the database generated code.

So drift will generate a variable named user which will be object of class $User. This variable needs to be used inside migration stregeies so that drift can successfully migrate a table.

Priyank Jain
  • 727
  • 1
  • 10
  • 15