are not-Long primary keys possible? - scala

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.

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.

Using a string vs an id as a foreign key in Mongodb

I have a collection users whose documents will belong to a company (and each company can have many users). Because I set a unique index on the company name, can I use the name as the foreign key inside the user document, or is it recommended to use the id instead?
If name is unique and is guaranteed to never change, then you can use it, no problem. Although there were cases in my practice when names turned out to be not-so-unique and not-so-immutable (damn requirement changes). So, just to be extra safe, use the id.

Query to database with 'primary key' on GoogleAppEngine?

I've made a guestbook application using Google App Engine(GAE):python and the client is running on iPhone.
It has ability to write messages on the board with nickname.
The entity has 3 fileds:
nickname
date
message
And I'm about to make another feature that user can post reply(or comment) on a message.
But to do this, I think there should a 'primary key' to the guestbook entity, so I can put some information about the reply on a message.
With that three fields, I can't get just one message out of database.
I'm a newbie to database. Does database save some kind of index automatically? or is it has to be done by user?
And if it's done automatically by database itself(or not), how can I get just one entity with the key??
And I want to get some advise about how to make reply feature generally also. Thanks to read.
Every entity has a key. If you don't assign a key_name when you create the entity, part of the key is an automatically-assigned numeric ID. Properties other than long text fields are automatically indexed unless you specify otherwise.
To get an entity if you know the key, you simply do db.get(key). For the replies, you probably want to use a db.ReferenceProperty in the reply entity to point to the parent message; this will automatically create a backreference query in the message to get replies.
Each entity has a key, it contains information such as the kind of entity it is, it's namespace, parent entities, and the most importantly a unique identifier (optionally user specifiable).
You can get the key of an entity using the key method that all entities have.
message.key()
A key can be converted to and from a URL-safe string.
message_key = str(message.key())
message = Message.get(message_key)
If the key has a user-specified unique identifier (key name), you can access it like this
message.key().name()
Alternatively, if a key name was not specified, an id will be automatically assigned.
message.key().id()
To assign a key name to an entity, you must specify it when creating the entity, you are not able to add/remove or change the key name afterwards.
message = Message(key_name='someusefulstring', content='etc')
message.put()
You will then be able to fetch the message from the datastore using the key name
message = Message.get_by_key_name('someusefulstring')
Use the db.ReferenceProperty to store a reference to another entity (can be of any kind)
It's a good idea to use key name whenever possible, as fetching from the datastore is much faster using them, as it doesn't involve querying.