Как создать таблицу без миграции версии в Android Room Persistence Library?

Нужно ли переносить базу данных, когда я просто создаю таблицу, не меняя другие таблицы и данные?

Как и в случае с гугл учебник и googled, в большинстве случаев говорилось, что мы должны перенести базу данных, если я создаю таблицу для каждого обновления, мне нужно увеличивать версию базы данных и миграцию, это очень сбивает с толку

2
0
1 211
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Да, это. Комната сравнивает схему базы данных при запуске, и если схемы разные, она запустит миграцию. Если схема будет другой и миграция не будет добавлена, вероятно, будет выбрано исключение IllegalStateException.

Более того, когда вы увеличили версию db и у вас нет изменений в схемах, вы должны добавить объект Migration в построитель. Если нет, Room очистит вашу базу данных и воссоздает их. Подробнее читайте в документации по классу RoomDatabase.java:

  /**
         * Adds a migration to the builder.
         * <p>
         * Each Migration has a start and end versions and Room runs these migrations to bring the
         * database to the latest version.
         * <p>
         * If a migration item is missing between current version and the latest version, Room
         * will clear the database and recreate so even if you have no changes between 2 versions,
         * you should still provide a Migration object to the builder.
         * <p>
         * A migration can handle more than 1 version (e.g. if you have a faster path to choose when
         * going version 3 to 5 without going to version 4). If Room opens a database at version
         * 3 and latest version is &gt;= 5, Room will use the migration object that can migrate from
         * 3 to 5 instead of 3 to 4 and 4 to 5.
         *
         * @param migrations The migration object that can modify the database and to the necessary
         *                   changes.
         * @return this
         */
        @NonNull
        public Builder<T> addMigrations(Migration... migrations) {
            mMigrationContainer.addMigrations(migrations);
            return this;
        }

 /**
         * Allows Room to destructively recreate database tables if {@link Migration}s that would
         * migrate old database schemas to the latest schema version are not found.
         * <p>
         * When the database version on the device does not match the latest schema version, Room
         * runs necessary {@link Migration}s on the database.
         * <p>
         * If it cannot find the set of {@link Migration}s that will bring the database to the
         * current version, it will throw an {@link IllegalStateException}.
         * <p>
         * You can call this method to change this behavior to re-create the database instead of
         * crashing.
         * <p>
         * Note that this will delete all of the data in the database tables managed by Room.
         *
         * @return this
         */
        @NonNull
        public Builder<T> fallbackToDestructiveMigration() {
            mRequireMigration = false;
            return this;
        }

Другие вопросы по теме