问题
I am building a small app with React Native and want to use Realm for persistency.
I defined the following schema for a Person:
const personSchema = {
name: 'Person',
properties: {
familyName: {type: 'string', optional: true},
givenName: {type: 'string'},
middleName: {type: 'string', optional: true},
emailAddresses: {type: 'list', objectType: 'string'},
}
}
export class Person {}
Person.schema = personSchema
The information as to how a Realm schema needs to be defined can be found in the Realm docs.
I then instantiate the DB like so:
const schemas = [Person]
const db = new Realm({schema: schemas})
However, there is an exception when reaching the last line giving me the following error:
Unhandled JS Exception: Migration is required due to the following errors:
- Target type
stringdoesn't exist for propertyemailAddresses.
I am using the iOS simulator for testing and deleted the app several times and then reinstalled it using the play button in Xcode.
Does anyone have an idea why I am getting this exception?
Update
I now created a separate schema for another db object: EmailAdress
const emailAddressSchema = {
name: 'EmailAddress',
properties: {
label: 'string',
email: 'string'
}
}
export class EmailAddress {}
EmailAddress.schema = emailAddressSchema
I also changed the objectType of emailAddresses in the personSchema to be of type EmailAddress now:
...
emailAddresses: {type: 'list', objectType: 'EmailAddress'},
...
Now, I am not getting the exception any more. Isn't it possible to add a property to a Realm class that is a list of strings?
回答1:
Lists of primitives are not yet supported in Realm. Until they are you need to wrap primitive types in an object as you have done with EmailAddress
来源:https://stackoverflow.com/questions/36516924/realm-react-native-migration-is-required-target-type-string-doesnt-exist-f