pinchbv / floor

The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications
https://pinchbv.github.io/floor/
Apache License 2.0
973 stars 191 forks source link

How to upgrade the database version #726

Closed yixiu30 closed 1 year ago

yixiu30 commented 1 year ago

In the test process, I added the field nickname for the first time. I added two more fields, chinesename and englishname, as required. But at this time, if I upgrade the database one version at a time, there is no problem, such as versions three to four. However, if I upgrade the database from the online version two to the latest version four, I will not report an error this is the example from three to four version, // create migration final migration1to2 = Migration(3, 4, (database) { database.execute('ALTER TABLE person ADD COLUMN chinesename TEXT'); database.execute('ALTER TABLE person ADD COLUMN englishnameTEXT'); }); final database = await $FloorAppDatabase .databaseBuilder('app_database.db') .addMigrations([migration1to2]) .build();

dkaera commented 1 year ago

Hey @yixiu30 Sorry, it's not clear what problem you hit with Floor. Basically the database version declares in @Database(version: <your_actual_version>) annotation. Migration which should be applied in the Migration(<from_version>, <to_version>, ....) object and the migrations are executed in order, one after the other 1 -> 2, 2 -> 3, 3 -> 4 if it's declared, of course Here is more info https://pinchbv.github.io/floor/migrations/

dkaera commented 1 year ago

and you lost the space between englishname and TEXT I'm not sure if this is an issue or just a sample code

yixiu30 commented 1 year ago

thank you very much! but i find anthor problem,you see this

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: DatabaseException(Error Domain=FMDatabase Code=1 "table chatrecord has no column named nameEn" UserInfo={NSLocalizedDescription=table chatrecord has no column named nameEn}) sql 'INSERT OR ABORT INTO chatrecord (chatListId, head_url, name, content, type, time, groupId, chatId, sendId, staffId, origingUrl, compressUrl, width, height, picture_local, title, fileType, filesize, filelocal, fileurl, isDownload, chatUuid, file_id, system, audioOpenEd, audioRecord, audioClick, dailyReportId, dailyReportType, isDailyReport, briefId, addContent, href, dialogHeight, upDateText, nameEn) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' args [https://test.xiamenjwzz.com:9999/portalfile/userPi..., weijiwo, , 1, 2023-01-03 16:43:20.945, 4872098147432661014, 225739, 4804247830982033430, 2fb135026aae410d81330a4162a6983d, https://files-1304239070.cos.ap-nanjing.myqcloud.c..., https://files-1304239070.cos.ap-nanjing.myqcloud.c..., 0, 0, , , , , , , 0, cb293d40-ba08-44e3-a8a2-36fac2f9e392, d5ffd44b596845268acb84e6ba20f7f2, 0, 0, , 0, , , 0, , report, , , , ]

0 wrapDatabaseException (package:sqflite/src/exception_impl.dart:11)

#1 SqfliteDatabaseMixin.txnRawInsert. (package:sqflite_common/src/database_mixin.dart:392) #2 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33) #3 SqfliteDatabaseMixin.txnSynchronized (package:sqflite_common/src/database_mixin.dart:344) #4 InsertionAdapter._insert (package:floor/src/adapter/insertion_adapter.dart:77) #5 InsertionAdapter.insert (package:floor/src/adapter/insertion_adapter.dart:28) #6 _$ChatRecordDao.insertChatRecord (package:flutterhtm/sql/database.g.dart:731) but the database update i have add the column nameEn final migration1to2 = Migration(7, 8, (database) async { await database.execute('ALTER TABLE chatrecord ADD COLUMN href TEXT NOT NULL DEFAULT "0";'); await database.execute('ALTER TABLE chatrecord ADD COLUMN dialogHeight TEXT NOT NULL DEFAULT "0";'); await database.execute('ALTER TABLE chatrecord ADD COLUMN upDateText TEXT NOT NULL DEFAULT "0";'); await database.execute('ALTER TABLE chatrecord ADD COLUMN nameEn TEXT NOT NULL DEFAULT "0";'); }); final database = await $FloorAppDatabase .databaseBuilder(GroupManager.dbName) .addMigrations([migration1to2]) .build();
dkaera commented 1 year ago

@yixiu30 Please check your database version in @Database annotation in your case, to execute the migration code above it should be 8

dkaera commented 1 year ago

no answer for a while