How to add property into existing model #220

Open
opened 2025-12-29 15:26:55 +01:00 by adam · 9 comments
Owner

Originally created by @shift00 on GitHub (Jun 28, 2018).

Hello, I need help 🆘
I have CoreStore without *.xcdatamodel files. It's working fine, but when I added new property to my model app returns crash from CS.

❗ [CoreStore: Fatal Error] BaseDataTransaction.swift:83 create
  ↪︎ Attempted to create an entity of type 'Obj1Model', but a destination persistent store containing the entity type could not be found.

I know that this is a configuration error, but I have no idea what to change to make it work.

Initialization code looks like this:

let dataStack: DataStack = DataStack(
            CoreStoreSchema(
                modelVersion: "V2",
                entities: [
                    Entity<Obj1CSModel>("Obj1Model"),
                    Entity<Obj2CSModel>("Obj2Model"),
                    Entity<Obj3CSModel>("Obj3Model")
                ]
            )
        )

CoreStore.defaultStack = dataStack

      let _ = CoreStore.addStorage(
            SQLiteStore(fileName: "DataStore.sqlite"),
            completion: { (_) -> Void in
                self.makeSomeMagicAfterCreatingDb()
        })

I changed modelVersion from V1 to V2, but this solution not working 😢

I think the topic is trivial, but I could not find the answer in the documentation.

Originally created by @shift00 on GitHub (Jun 28, 2018). Hello, I need help 🆘 I have CoreStore without *.xcdatamodel files. It's working fine, but when I added new property to my model app returns crash from CS. ``` ❗ [CoreStore: Fatal Error] BaseDataTransaction.swift:83 create ↪︎ Attempted to create an entity of type 'Obj1Model', but a destination persistent store containing the entity type could not be found. ``` I know that this is a configuration error, but I have no idea what to change to make it work. Initialization code looks like this: ```swift let dataStack: DataStack = DataStack( CoreStoreSchema( modelVersion: "V2", entities: [ Entity<Obj1CSModel>("Obj1Model"), Entity<Obj2CSModel>("Obj2Model"), Entity<Obj3CSModel>("Obj3Model") ] ) ) ``` ` CoreStore.defaultStack = dataStack` ```swift let _ = CoreStore.addStorage( SQLiteStore(fileName: "DataStore.sqlite"), completion: { (_) -> Void in self.makeSomeMagicAfterCreatingDb() }) ``` I changed modelVersion from V1 to V2, but this solution not working 😢 I think the topic is trivial, but I could not find the answer in the documentation.
Author
Owner

@JohnEstropia commented on GitHub (Jun 28, 2018):

You'll need to keep both V1 and V2 entities and add them both to your DataStack. CoreStore needs to know all your old models as well as your latest model for it to infer migrations.
See https://github.com/JohnEstropia/CoreStore#declaring-model-versions, especially the section CoreStoreSchema-based model versions with progressive migration.

@JohnEstropia commented on GitHub (Jun 28, 2018): You'll need to keep both V1 and V2 entities and add them *both* to your `DataStack`. CoreStore needs to know all your old models as well as your latest model for it to infer migrations. See https://github.com/JohnEstropia/CoreStore#declaring-model-versions, especially the section **CoreStoreSchema-based model versions with progressive migration**.
Author
Owner

@shift00 commented on GitHub (Jun 28, 2018):

@JohnEstropia, thanks for your fast reply.
Ok, I have DataStack with two CoreStoreSchemas (I love github code formatting...)

  let dataStack: DataStack = DataStack(
CoreStoreSchema(
                modelVersion: "V1",
                entities: [
                    Entity<V1.Obj1CSModel>("Obj1"),
                    Entity<V1.Obj2CSModel>("Obj2"),
                    Entity<V1.Obj3CSModel>("Obj3")
                ]
            ),
            CoreStoreSchema(
                modelVersion: "V2",
                entities: [
                                        Entity<V2.Obj1CSModel>("Obj1"),
                    Entity<V2.Obj2CSModel>("Obj2"),
                    Entity<V2.Obj3CSModel>("Obj3")
                ]
            ),
            migrationChain: ["V1", "V2"]
        )

next, I have:
CoreStore.defaultStack = dataStack

and this magic writed with documentation:

        let migrationProgress: Progress? = try CoreStore.addStorage(
            SQLiteStore(
                fileName: "DBStore.sqlite",
                configuration: "Config2"
            ),
            completion: { (result) -> Void in
                switch result {
                case .success(let storage):
                    print("Successfully added sqlite store: \(storage)")
                case .failure(let error):
                    print("Failed adding sqlite store with error: \(error)")
                }
        }
        )
      migrationProgress?.setProgressHandler { [weak self] (progress) -> Void in
            "Progress: \(progress.fractionCompleted), Description: \(progress.localizedDescription), Step: \(progress.localizedAdditionalDescription)".log(.info)
        }

And this solution not working 😞 where did I make a mistake?

@shift00 commented on GitHub (Jun 28, 2018): @JohnEstropia, thanks for your fast reply. Ok, I have DataStack with two CoreStoreSchemas (I love github code formatting...) ```swift let dataStack: DataStack = DataStack( CoreStoreSchema( modelVersion: "V1", entities: [ Entity<V1.Obj1CSModel>("Obj1"), Entity<V1.Obj2CSModel>("Obj2"), Entity<V1.Obj3CSModel>("Obj3") ] ), CoreStoreSchema( modelVersion: "V2", entities: [ Entity<V2.Obj1CSModel>("Obj1"), Entity<V2.Obj2CSModel>("Obj2"), Entity<V2.Obj3CSModel>("Obj3") ] ), migrationChain: ["V1", "V2"] ) ``` next, I have: `CoreStore.defaultStack = dataStack` and this magic writed with documentation: ```swift let migrationProgress: Progress? = try CoreStore.addStorage( SQLiteStore( fileName: "DBStore.sqlite", configuration: "Config2" ), completion: { (result) -> Void in switch result { case .success(let storage): print("Successfully added sqlite store: \(storage)") case .failure(let error): print("Failed adding sqlite store with error: \(error)") } } ) ``` ```swift migrationProgress?.setProgressHandler { [weak self] (progress) -> Void in "Progress: \(progress.fractionCompleted), Description: \(progress.localizedDescription), Step: \(progress.localizedAdditionalDescription)".log(.info) } ``` And this solution not working 😞 where did I make a mistake?
Author
Owner

@JohnEstropia commented on GitHub (Jun 28, 2018):

@shift00 Can you try this again without the configuration: "Config2" argument? If you aren't using that feature you can just pass nil.

@JohnEstropia commented on GitHub (Jun 28, 2018): @shift00 Can you try this again without the `configuration: "Config2"` argument? If you aren't using that feature you can just pass `nil`.
Author
Owner

@shift00 commented on GitHub (Jun 28, 2018):

@JohnEstropia Nope, it still does not work. The code from the success/failure case section does not call.

@shift00 commented on GitHub (Jun 28, 2018): @JohnEstropia Nope, it still does not work. The code from the success/failure case section does not call.
Author
Owner

@JohnEstropia commented on GitHub (Jun 28, 2018):

Are you working on freshly migrated data or are you running this after you got the ❗ [CoreStore: Fatal Error]?
Your previous code and your new code have totally different entity names for the V1 classes. The V1 entities should be exactly the same as your old one.

(also fixed your code formatting 😄 )

@JohnEstropia commented on GitHub (Jun 28, 2018): Are you working on freshly migrated data or are you running this after you got the `❗ [CoreStore: Fatal Error]`? Your previous code and your new code have totally different entity names for the V1 classes. The V1 entities should be exactly the same as your old one. (also fixed your code formatting 😄 )
Author
Owner

@shift00 commented on GitHub (Jun 28, 2018):

I carried out two tests
first: remove installed app from device -> install fresh instance of my app - working 👍
second: remove installed app from device -> install fresh instance of old version app (without V2 and changes for migration) -> working -> install instance of new version app (with new mechanism and V2 models) - not working 👎

It is possible that addStorage will not update models in the database if it exists?

@shift00 commented on GitHub (Jun 28, 2018): I carried out two tests first: remove installed app from device -> install fresh instance of my app - working 👍 second: remove installed app from device -> install fresh instance of old version app (without V2 and changes for migration) -> working -> install instance of new version app (with new mechanism and V2 models) - not working 👎 It is possible that `addStorage` will not update models in the database if it exists?
Author
Owner

@JohnEstropia commented on GitHub (Jun 29, 2018):

It is possible that addStorage will not update models in the database if it exists?

We have multiple projects that use this migration feature without issues.

Do you set the VersionLock for your V1 models? When you run your app's old version, check the console logs for the VersionLock and take note of the hashes for each entity. (See https://github.com/JohnEstropia/CoreStore#versionlocks)

Once you have the hashes for your old version, you can try your app's new version again adding the versionLock for V1 as a parameter to CoreStoreSchema. You will know if you accidentally edited your V1 because CoreStore will check the hashes beforehand.

@JohnEstropia commented on GitHub (Jun 29, 2018): > It is possible that addStorage will not update models in the database if it exists? We have multiple projects that use this migration feature without issues. Do you set the `VersionLock` for your V1 models? When you run your app's old version, check the console logs for the VersionLock and take note of the hashes for each entity. (See https://github.com/JohnEstropia/CoreStore#versionlocks) Once you have the hashes for your old version, you can try your app's new version again adding the versionLock for V1 as a parameter to `CoreStoreSchema`. You will know if you accidentally edited your V1 because CoreStore will check the hashes beforehand.
Author
Owner

@shift00 commented on GitHub (Jun 29, 2018):

Unfortunately, the VersionLock does not return any errors in the V1 model 😭 I am sure that to solve the problem, just change one line. I do not know where yet 😆

@shift00 commented on GitHub (Jun 29, 2018): Unfortunately, the `VersionLock` does not return any errors in the V1 model 😭 I am sure that to solve the problem, just change one line. I do not know where yet 😆
Author
Owner

@lqmios commented on GitHub (Dec 26, 2019):

@shift00 I had the same problem,Have you solved your problem?

@lqmios commented on GitHub (Dec 26, 2019): @shift00 I had the same problem,Have you solved your problem?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#220