Datanucleus: JDO Deprecated field (do not create, read if exists) - metadata

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

Related

Creating new docs without auto-ids without accidentally referencing an existing doc

If I have a collections of docs in which I provide the ids (i.e. not the Firestore auto ids), how can I be certain to create a new doc rather than referencing an existing one?
For instance if I have a collection foo with a document id bar, I can reference it as (given db is the injected AngularFirestore service) this.db.doc('foo/bar').
Then later I want to create another foo doc, I can check if it already exists with something like
checkExits(id: string): Observable<boolean> {
const docRef = this.db.doc(`foo/${id}`);
return docRef.snapshotChanges().map(change => change.payload.exists);
}
However in the time that I check that it exists, to creating the doc, isn't it possible that it gets created? Such that when I try to create the new doc, it is instead referencing an existing one and overwriting it? i.e. this.db.doc('foo/bar').set({})
How can I create new docs specifying the id, while avoiding referencing existing docs (and throwing an error if it exists).
You'll want to use a transaction for that. Within the transaction you first call get() on the location, to ensure nothing exists yet. Then you write to the same location, to create the document.

Get Mongoose to automatically create joined document

I have 2 Mongoose.js Schemas that work together with the 'populate' feature: A 'user' schema and another based on their role. E.g. 'admin'. When a user is assigned a role, a corresponding document needs to be created in a different collection with a link to the _id of the document in the users collection. (Yes, more like an SQL database than non-relational, I know)
Currently I manually create the second document in my code whenever a user with a specialized role is created or a role is added to a user. |
I'd like to know if there is a way to automatically create this corresponding record from my schema whenever a 'user' document is created or updated with a role.
Any advice?
Nothing will do it automatically, but you can use the mongoose middleware to insert our update a document in another collection pre or post save.
The post hook will have the _id populated.
If you want to do it in the pre hook (to enforce some transactional integrity) you can assign the _id manually.

MongoEngine: Documents disappear after allowing_inheritance

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.

How to Query a Read-Only Field with ORMLite

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.

web2py Validation

I would like to know validation is a must, on fields which are not present on the form but are available in the table. Does marking them as NULL in the define_table make them validated only when they are present in the form?
The form validators apply only to forms, so will not affect fields that are not present in the form. I'm not sure what you mean by marking a field as NULL, but if you are referring to Field(..., notnull=True), that executes the SQL NOT NULL statement when the database table is first created (assuming DAL migrations are enabled). That option is enforced by the database itself whenever a record is inserted or updated (via a form or any other method). If a notnull field is left empty, it will result in an operational error from the database.