如何更改 table 并添加 onDelete 级联约束 sqlite
How to alter a table and add onDelete cascade constraint sqlite
我正在使用房间数据库 我有一个 table,我忘记在其中添加 onDelete = CASCADE
之前的版本
@Entity(indices = {@Index(value = {"info_id"})}, foreignKeys = @ForeignKey(entity = StudentClass.class, parentColumns = "id", childColumns = "info_id"))
我现在想要的
@Entity(indices = {@Index(value = {"info_id"})}, foreignKeys = @ForeignKey(entity = StudentClass.class, parentColumns = "id", childColumns = "info_id", onDelete = CASCADE))
我正在尝试迁移数据库
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("What should I do here to add onDelete = CASCADE");
}
};
好的,我终于找到了解决方案
database.execSQL("CREATE TABLE attendance_temp(attendanceId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, time INTEGER NOT NULL, student_id INTEGER, student_name TEXT , roll_number INTEGER, image_path TEXT, info_id INTEGER, present INTEGER , CONSTRAINT fk_class_info FOREIGN KEY (info_id) REFERENCES class_info(id) ON DELETE CASCADE)");
database.execSQL("INSERT INTO attendance_temp SELECT * FROM Attendance");
database.execSQL("DROP TABLE Attendance");
database.execSQL("ALTER TABLE attendance_temp RENAME TO Attendance");
database.execSQL("CREATE INDEX index_Attendance_info_id ON Attendance(info_id)");
还将实体 class 中的架构更改为
@Entity(indices = {@Index(value = {"info_id"})}, foreignKeys = @ForeignKey(entity = StudentClass.class, parentColumns = "id", childColumns = "info_id", onDelete = CASCADE))
顺序很重要。
SQLite不完全支持SQL ALTER TABLE标准:
Only the RENAME TABLE, ADD COLUMN, and RENAME COLUMN variants of the
ALTER TABLE command are supported. Other kinds of ALTER TABLE
operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so
forth are omitted.
所以添加约束的唯一方法是重新创建 table 并将旧的数据复制到新的:
// 1. Rename your table
database.execSQL("ALTER TABLE Entity RENAME TO Entity_old")
// 2. Recreate the same entity, but now with the constraint
database.execSQL("""
CREATE TABLE Entity(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
parentId INTEGER,
someField STRING,
CONSTRAINT fk_Entity_entityId_EntityParent_id FOREIGN KEY (entityId) REFERENCES EntityParent(id) ON DELETE CASCADE
)
""")
// 3. Copy the data from the old table
database.execSQL("INSERT INTO Entity SELECT * FROM Entity_old")
// 4. Delete old table
database.execSQL("DROP TABLE Entity_old")
// 5. Recreate the indices
database.execSQL("CREATE INDEX index_Entity_id ON Entity(id)")
我正在使用房间数据库 我有一个 table,我忘记在其中添加 onDelete = CASCADE
之前的版本
@Entity(indices = {@Index(value = {"info_id"})}, foreignKeys = @ForeignKey(entity = StudentClass.class, parentColumns = "id", childColumns = "info_id"))
我现在想要的
@Entity(indices = {@Index(value = {"info_id"})}, foreignKeys = @ForeignKey(entity = StudentClass.class, parentColumns = "id", childColumns = "info_id", onDelete = CASCADE))
我正在尝试迁移数据库
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("What should I do here to add onDelete = CASCADE");
}
};
好的,我终于找到了解决方案
database.execSQL("CREATE TABLE attendance_temp(attendanceId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, time INTEGER NOT NULL, student_id INTEGER, student_name TEXT , roll_number INTEGER, image_path TEXT, info_id INTEGER, present INTEGER , CONSTRAINT fk_class_info FOREIGN KEY (info_id) REFERENCES class_info(id) ON DELETE CASCADE)");
database.execSQL("INSERT INTO attendance_temp SELECT * FROM Attendance");
database.execSQL("DROP TABLE Attendance");
database.execSQL("ALTER TABLE attendance_temp RENAME TO Attendance");
database.execSQL("CREATE INDEX index_Attendance_info_id ON Attendance(info_id)");
还将实体 class 中的架构更改为
@Entity(indices = {@Index(value = {"info_id"})}, foreignKeys = @ForeignKey(entity = StudentClass.class, parentColumns = "id", childColumns = "info_id", onDelete = CASCADE))
顺序很重要。
SQLite不完全支持SQL ALTER TABLE标准:
Only the RENAME TABLE, ADD COLUMN, and RENAME COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.
所以添加约束的唯一方法是重新创建 table 并将旧的数据复制到新的:
// 1. Rename your table
database.execSQL("ALTER TABLE Entity RENAME TO Entity_old")
// 2. Recreate the same entity, but now with the constraint
database.execSQL("""
CREATE TABLE Entity(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
parentId INTEGER,
someField STRING,
CONSTRAINT fk_Entity_entityId_EntityParent_id FOREIGN KEY (entityId) REFERENCES EntityParent(id) ON DELETE CASCADE
)
""")
// 3. Copy the data from the old table
database.execSQL("INSERT INTO Entity SELECT * FROM Entity_old")
// 4. Delete old table
database.execSQL("DROP TABLE Entity_old")
// 5. Recreate the indices
database.execSQL("CREATE INDEX index_Entity_id ON Entity(id)")