I have a MongoEngine Document that previously didn't allow inheritance. I then wanted to inherit from it, so I set {allow_inheritance:True}. As soon as I did that the existing documents for that model didn't appear anymore when calling <myModel>.objects. If I momentarily set {allow_inheritance:False} then the documents come back. Why would that be?
I'm using MongoEngine verison 0.8.7
Figured it out. When using allow_inheritance, MongoEngine stores a special _cls field in the base document with the name of the class, or derived class. So for your BaseClass it would store "BaseClass" as the value, and for your DerivedClass it would would store "BaseClass.DerivedClass" as the value. But without allow_inheritance set initially, it does not have this special _cls field set. So after setting allow_inheritance I had to go in to the mongo field manually (not through mongoengine) and perform an update to add the _cls field with the BaseClass value and then documents reappeared.
Related
I'm currently trying to work out the best way to architect my realm objects for ease of retrieval.
I have 2 objects tags and object there are multiple tags and each one might contain many object. Similarly each object could have multiple tag associated with it
Ideally selecting a single tag should retrieve all object that have at least that one tag (but could obviously have multiple)
would my models be specified as
class Tag: Object {
let objects = List<Object>()
}
class Object {
let tags = List<Tag>()
}
I don't think I need to use an inverse relationship here or should I? Choosing a Category I should be able to just retrieve a list of all object references regardless, but then maintaining and updating the references to an object might be difficult here? I.e a user selects tag 'A' then updates the first object to also include tag 'B' I would need to update the object in the List for Tag A, then add a new item to the list for Tag 'B' and finally update the actual Object itself to include Tag 'B' in it's list of tags.
Just to be clear an Object will only ever display and allow editing of it's Tag objects. But the Tag object itself will need to know what Object's are applicable to it.
However it feels like I will have to do multiple updates when ideally I'd like to minimise this effort. Can anyone recommend a better way to do this? Or is there no way around this due to the limitations of Realm?
This is exactly what LinkingObjects is for. Changing the objects property in Tag to let objects = LinkingObjects(fromType: Object.self, property: "tags") will make it automatically update whenever a tag is added to an object.
I am trying ORMLite as an ORM for a project I am developing. I am mapping a java class to a table that has some auditing fields (ie. updatedby, updatedtime, etc.). The auditing fields are maintained by the database using triggers to ensure that no matter what front-end the user is using these fields will always be correctly updated when a record is updated.
I need to include these fields in my client application to inform the user when the record was last updated, but the user can't change them. Is there a way to annotate the class so that ORMLite won't try to perform updates on these fields or include them in insert statements. The database will deny an update if these fields are included in an update statement (which is why I can't just write back the original value that was queried from the database).
I tried using the #DatabaseField(persisted = false) annotation on the Java fields, but then they don't get queried at all so the Java object is never populated with these fields.
Basically, I need these fields to be included in SELECT statements, but not included in INSERT or UPDATE statements (equivalent to a #DatabaseField(immutable = true) annotation).
Interesting pattern. ORMLite didn't support the feature at the time but now it does as of version 4.46.
There is now a #DatabaseField(readOnly=true) annotation field.
Scenario: A Field is deprecated.
Is it possible to map the field in a way that:
It's not created in the database even if "datanucleus.autoCreateColumns" is set to TRUE (override by field attribute)
It's loaded from the database (if exists)
Thanks
I'm calling the RestAPI methods to update an object https://parse.com/docs/rest#objects-updating and I'm getting a success response, but the objetId won't change.
The objectId is a non-editable field so you won't be able to update it. If you need to use your own ID, you will have to add a separate field in addition to the objectId field.
I'm not sure you can change objectId. It's a unique identifier assigned by the system at instantiation.
It's not possible unless you import a class. You cannot import a class if a class by the same name already exists so you will need to merge that data first.
Example: I read data from an XML file. This data has unique id elements. I want to store those objects with their original unique id. How would I do that?
I figured out I could ask the managed object for it's ID, like this:
NSManagedObjectID *moID = [managedObject objectID];
but here the problem is: The XML tells me with the id element which object this is, and I need to look up in the database of core data if this object already exists in there, or not. So is it the only option to make an id attribute in my managed object model for that entity and then query for that? Then I will have two id systems right?
Don't worry about the ObjectID of Core Data. This is an internal unique ID which is not guarantied to be constant during the object's life cycle (e.g. it will change when you save the object to sql store). Just create a new mandatory attribute in your model and flag it as indexed so retrieval will be fast.
In the entity associated to this kind of objects, simply add another attribute of type string, call it objectID or similar and declare it to be mandatory.