Realm Database open fails after adding required property to existing object schema
0 reputation · 25 Jul 2021, 17:02 UTC
0 reputation · 25 Jul 2021, 17:02 UTC
Goal: confirm whether an existing Realm Database file can be opened after a required non-nullable property is added to an object model without supplying a default value and without defining an explicit migration block.
Realm tracks an internal schema version, and documentation indicates migration blocks are required for changes beyond purely additive updates. Adding a required property may leave previously persisted objects without a value, but the exact failure point—schema validation at open, first object read, or write—is not consistently specified across SDK versions. Behavior also differs by language SDK: Swift and Kotlin typically use explicit migration callbacks, while Java and .NET may allow automatic additive updates in some configurations. The question is version-dependent, so the target SDK and release must be identified before the failure mode can be diagnosed.
Unresolved: does opening the Realm throw, does the failure surface lazily on read, or is behavior SDK-specific? Is an explicit migration block mandatory when the only change is a required property without a default?
35775 reputation · 26 Jul 2021, 01:48 UTC
No. Adding a required non-nullable property to an existing Realm object schema without a default value and without an explicit migration block is not a safe schema change. Whether the failure happens when the Realm file is opened or only when an existing object is first read depends on the Realm SDK and version. There is no cross-SDK guarantee that Realm.open() will succeed.
Realm records an internal schema version with the database file. When the object model changes, the configuration’s schemaVersion must reflect that change. If the stored version and the configured version disagree, Realm reports a mismatch at open. If they agree but pre-existing objects lack the new required property, accessing that property can throw or return null, again depending on the SDK.
The research brief notes that Swift and Kotlin SDKs tend to be stricter at open or first access, while Java and .NET may allow the file to open but fail when the property is read. These are version-dependent patterns, not verified facts for your specific installation, so treat them as assumptions pending your own test.
schemaVersion.Open a copy of the Realm file with the updated configuration and catch exceptions at two points: during open() and during the first read of the new property on an existing object. Record the exception type, message, and stack location. Repeat on each SDK your application supports.
Which Realm SDK and version are you using? This determines whether the failure surfaces at open or lazily on first property access, and which migration syntax applies.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.