Comments (6)
It does seem tho that looking up this family like this:
FamilyModel.first({id: query, disabled: false}, {includes: ['users', 'familyMemberships']}, function (err, familyModel) {
if (err) return callback.call(this, createResponse.error());
if (!familyModel) return callback.call(this, createResponse.error(404, 'No family found'));
console.log(familyModel)
});
returns my expected
{
"message": "No family found"
}
from model.
oh...I think I may know what it is... is afterCreate supposed to fire even on reads? It appears as tho it's firing "create" on associated models. This is why it works if you hit it directly, but if you ask for the association its like its creating it new. This also means all my default props im setting in afterCreate are overriding whats actually in the DB
from model.
Yes, create fires even on reads. I'm not sure if this is correct behavior anymore, since we now have the reify
scenario.
Since the create
method doesn't actually save models to the database (you still have to call save), create
is really more of an initialize
method, so the behavior is correct if you look at it that way.
See here, where models are initialized during a query using the create method.
I agree that this behavior is confusing though. What do you think @mde?
from model.
@ben-ng afterCreate tho is the suggested way of setting defaults as far as i know. Is there some other method or way in the model to set defaults on create?
The hardest part is that this fires only on associated models. If I read Foo model it doesn't fire. If I read Foo model with Bar and Baz associations then Bar and Baz get the create but Foo doesn't. At least if Foo did it then I could account for that a little easier, but currently I have to come up with a way to know if its being called as an association or as a "top level" model.
from model.
So, at least for now and in my case, I'm getting around it with a tiny module:
module.exports = function (prop, def) {
return prop === undefined ? def : prop;
};
I use it like
var setAsModelDefault = require('../utils/set-as-model-default');
this.afterCreate = function () {
this.disabled = setAsModelDefault(this.disabled, false);
};
Not ideal, but it works for me
from model.
Ahh i see. I'll take a look at making the behavior consistent then. I think the right thing to do is probably to not fire create on reads anymore. Its going to be a breaking change, but its the most logical to me. We should have an init event take its place. Then, init will happen on both queries and user-performed initializations, create only on user-performed initializations, and reify on queries.
from model.
Related Issues (20)
- How do I save data to association models? HOT 5
- Null character in text body crashes PG adapter HOT 5
- Real number not always a good choice. HOT 3
- createdAt and updatedAt HOT 1
- New Eager Loading Bug! HOT 6
- PG Adapter Presently Only Fakes Streaming HOT 7
- TypeError: Cannot call method 'collection' of undefined. HOT 4
- Redis adapter HOT 1
- PG 9.3: ERROR column "foobar.created_at" must appear in the GROUP BY clause or be used in an aggregate function HOT 1
- some questions about update
- Complex query question HOT 1
- why finally sql's tablename have `s`or `es` HOT 1
- Can you only updateProperties per model? HOT 10
- Filter which properties are shown HOT 7
- Rest-adapter: Instance cache lifetime HOT 1
- How to write a custom adapter HOT 1
- cast json fields to string before applying queries
- about `save` method and model's id HOT 3
- Save Method issue
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from model.