ActiveAndroid 架构迁移添加列是一个对象

ActiveAndroid schema migration add column that is an object

我对 SQLite 和 ActiveAndroid 还很陌生。

我正在努力推出应用更新,需要迁移我的数据库。一些表已经添加或删除了列等。在 ActiveAndroid 中,模式迁移是通过编写迁移脚本来执行的。在处理基本数据类型时,我很容易理解如何执行此操作,但我很困惑如何添加映射到其他 objects/classes.

的列

例如,下面是一个现有模型:

@Table(name = "GatewayDevices", id = BaseColumns._ID)
public class GatewayDevice extends Model {

    private static final String TAG = GatewayDevice.class.getSimpleName();


    @Column(name = "identifier", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE, notNull = true)
    private String identifier;

    @Column(name = "deviceType", notNull = true)
    private String deviceType;

    @Column(name = "name")
    private String name; 

    public GatewayDevice() {
        super();
    }

    ...

}

我正在将其更新为:

@Table(name = "GatewayDevices", id = BaseColumns._ID)
public class GatewayDevice extends Model {

    private static final String TAG = GatewayDevice.class.getSimpleName();


    @Column(name = "identifier", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE, notNull = true)
    private String identifier;

    @Column(name = "deviceType", notNull = true)
    private String deviceType;

    @Column(name = "name")
    private String name;

    @Column(name = "controller", onUpdate = Column.ForeignKeyAction.CASCADE, onDelete = Column.ForeignKeyAction.CASCADE)
    private Controller controller;


    public GatewayDevice() {
        super();
    }

    ...

}

如何使用基本 ALTER TABLE GatewayDevice ADD COLUMN controller <type> 结构添加此列?

您可以为 Controller class 编写自己的序列化程序,并进行迁移

ALTER TABLE GatewayDevice ADD COLUMN controller TEXT

(字符串类型)