Within Realm how do you perform a Migration that adds the Primary Key function to a property, when using Swift?
This was previously answered for Realm Objective-C here: https://stackoverflow.com/a/29417579/599344
Essentially, if you're promoting a property to a primary key, and the property already has a unique value per object, you don't need to do anything special aside from running a normal migration.
If your new primary key property does have duplicate entries, then you'll need to change their values to something unique inside the migration block.
Related
I have several cases where I have to update some object models, including the property that I use as a primary key.
For example :
Merge the primary key name (e.g. Georges, Anna...), and the familyName (e.g. Johnson, Smith...) property, and use that new merged name (e.g. Georges Johnson...) as a primary key.
Make the identifier primary key from type Int to type String
But of course the documentation clearly states that :
Once an object with a primary key is added to a Realm, the primary key cannot be changed
You can always remove old objects and create new ones, but this would add a lot of complexity to re-create the relationships.
And I'm pretty sure realm may not be happy with the identifier type change either way (judging by the thrown exceptions that I encountered).
So I was wondering if there was a simpler way to do so, or if I had to do a lot of manual grunt work to achieve my very simple goals.
Katsumi from Realm here. Realm supports primary key migration.
Primary keys can be changed only during migration. So you'd like to change the existing primary key values, you can write migration block, then you can assign new values for each new objects. The values must be unique of course.
let config = Realm.Configuration(schemaVersion: 1, migrationBlock: { (migration, schemaVersion) in
migration.enumerateObjects(ofType: "Person", { (oldObject, newObject) in
newObject!["key"] = ...
})
})
You can change primary key type as well.
In that case, you also need to write a migration block and assign new values. Because the primary key property is cleared when changing the type. Also, you can merge or split existing primary key property. You can add a new property, then specify it as a primary key, then you also should write migration block and assign new unique values as well.
However, the latest version of Realm (2.8.0 and 2.8.1) made unintentional bug that doesn't allow to modify primary key even during migration. So if you're urgent, you should use the previous version (2.7.x), if you are not urgent, please wait to be fixed the bug in next release.
Lastest Realm Swift 1.0.1
I do follow this
composite primary key realm/swift
It's shows error following
How to i can write with Swift?
You will need to drop the lazy specifier from the variable declaration as how the error messages states such properties are not supported. You don't want to ignore the property as you want to use it as a primary key.
Beside that I would recommend to separate your integer components within your compound key. (e.g. "\(id)|\(tourId)") Otherwise you can end up having different objects with the same conflicting primary key. e.g. You could have objects with compoundKey="123" for the combination id=1 tourId=23, but also for id=12 tourId=3.
Is it possible to define not-Long primary key?
Motivation: I have a set of XML files to convert to rdb. String attributes are used as unique keys.
Not possible.
From docs:
What should you do when you need to specify the id yourself?
Nothing. You shouldn’t do that. The id property is supposed to be generated and managed by db only. If you need to specify some external unique identifier, like, for instance, Amazon’s ASIN, just add an appropriate field to your entity and specify it as unique on SORM instantiation.
This is used with the Property(() => p).HasDatabaseGeneratedOption() call. Is is perhaps to turn off default DB value generation?
EF uses DatabaseGeneratedOption to figure out what to do with the value of a key column for new entities. If the DatabaseGeneratedOption is Identity EF knows that the value the property is set to can be ignored and that the one that comes from the database should be used. If the DatabaseGeneratedOption is None EF will insert the value of the property to the database as the value of the key column.
In Code First - when Code First conventions find an int property that can be the key property for the given entity they by default will configure this column as identity column (meaning the database will generate the value of the key column/property). DatabaseGeneratedOption.None allows you to overwrite this if you want to set key values on your own.
Its effect is to configure EF to not fetch a new identity value after inserting into the database.
I'm getting more and more frustrated with EF...
I have a table called ExtendedField with:
Record
DocRef
DocType
Name
Record is the primary key
DocRef and DocType are foreign keys used to identify which Ticket they belong to
Name is the key used by the "definition" table to define what the field actually is
So basically I need 2 associations:
One between Ticket and ExtendedField
on ExtendedField.DocRef=ticket.record
and
ExtendedField.docType=HeaderDocType
One between Definition on
ExtendedField.Name=Definition.FieldName
Then I still need Record to be the primary key so I can directly access the fields.
As near as I can tell this is impossible to do in Entity Framework. For every association all the keys need to be mapped together, whereas I need two keys for one association, 1 key for another one and the actual primary key wouldn't be used in any associations.
It doesn't appear that you can define an association between fields that aren't entity keys either.
So is there any way to do this? Am I missing something?
It's a v1, bro. I myself have had some major pain with mapping of key constraints in EF. I hear that better things are coming in v2.