ios - Realm swift change primaryKey -
so have realm object
class registrationplatedb: rlmobject { dynamic var registrationplate : string = "" dynamic var user : string = "" override static func primarykey() -> string? { return "registrationplate" } ...
and change
class registrationplatedb: object { dynamic var plateid : int = -1 dynamic var registrationplate : string = "" dynamic var name : string = "" dynamic var user : string = "" override static func primarykey() -> string? { return "plateid" } ....
so i've written migration
migration.enumerate(registrationplatedb.classname()) { oldobject, newobject in newobject!["name"] = "" newobject!["user"] = "" newobject!["registrationplate"] = "" newobject!["plateid"] = -1 newobject!["primarykeyproperty"] = "plateid"; }
but error because of primarykey change, since if leave line out works primary key doesn't change.
can give me idea how change primarykey.
edit: first object written objective-c realm
edit2: or if know how make plateid autoincrement
katsumi realm here. don't need attempt change primary key in migration block.
we automatically update schema latest version, , thing have handle in migration block adjusting data fit (e.g. if rename property have copy data old property new 1 in migration block).
so newobject!["primarykeyproperty"] = "plateid";
not needed.
i think migration block should following:
migration.enumerate(registrationplatedb.classname()) { oldobject, newobject in newobject!["user"] = oldobject!["user"] newobject!["registrationplate"] = oldobject!["registrationplate"] newobject!["plateid"] = int(oldobject!["registrationplate"] as! string) }
if you'd assign sequence numbers plateid
, example:
var plateid = 0 migration.enumerate(registrationplatedb.classname()) { oldobject, newobject in newobject!["user"] = oldobject!["user"] newobject!["plateid"] = plateid plateid += 1 }
Comments
Post a Comment