composite primary key with realm 1.0.1 on swift - swift

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.

Related

Apache Ignite generated key for cluster but no key class

I used Ignite Web Console to generate a cluster configuration for an existing database. One of the tables in question has no key--it consists of two columns, both integers, neither of which is a key. There is a foreign key constraint that one of the columns must exist in another table, but I don't especially care about that.
In the generated cluster xml, each of the two columns is represented as a value field. These two fields match up with the generated POJO class as well. However, in the "keyType" field of the cluster config, it references a generated key class that, as far as I can tell, does not exist. If the POJO class for the table is Foo, then the key class is written down as FooKey, but this class does not exist in the project, and there is no definition for what fields would be in the key.
What am I supposed to do when referencing this cache? Do I need to create an implementation of this key class myself? When I make calls to the cache, does it need to be in the Entry format? How does the key-value store work when there is no key in the original table?
I think you’ll need to add these fields manually to "keyType". In order to do this find a model in Advanced -> SQL Scheme, then select two columns in "Key fields" dropdown menu. This will generate the FooKey.

Primary key update in Realm migration

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.

Perform a Realm Migration that adds primary key

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.

are not-Long primary keys possible?

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.

How can you make an association without using all entity keys in entity framework?

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.