New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom getters called twice in a row? #1234
Comments
That line should not be the issue, it's just providing BC for .values. |
How are you retrieving the data from the model? I.e. are you using .values/.get/.toJSON() ? |
If you can provide me with a self-contained test case, i'll get this fixed for you today. |
Yep! I did that logging, the first time I get a string " |
Appreciate it! It might take a little time -- we're using coffeescript, custom helper functions that create the Sequelize classes, etc. so I had to unwind the code a bit to create that example. I'll see if I can extract a simple example! |
Well this issue definitely needs a real fix. |
Alright, i'll just need a model definition and the calls you're making to produce this error (create -> find, etc). Well yeah, a unit test - Then it should be simple for me to track down and fix this. |
Just in case it's not clear from my example - my DB field is backed by type Sequelize.STRING, so in the first call, I'm getting what I expect from this.dataValues(field), a string. And then I change its type when I parse that string into an object. Which might be the problem -- or at least revealing a kind of boundary case where inbetween the first call (hydration) and the 2nd (convert toJSON) fields of this type aren't consistently used as either pre-hydrated or post-hydrated? if that makes sense... |
I see that - But since it's a getter, it shouldn't affect the raw value - Unless node suddenly magically passes strings by reference now :) |
nope, and I'm not writing back into the datavalues or anything! :) |
Setters and getters should be completely ignored when hydrating from the database, atleast that's the intent. |
But yeah, somethings off - A test case to prove this from start to end would be great - Then i can fix. (If you feel confident you could just create a new sample file/model and see if you can reproduce it there). |
right, that's why I called out the line I did -- that function appears to be called during hydration, and in turn, it appears to call all the customGetters |
ok! working on a repro case now - do you happen to have a sample/skeleton I could drop a model into? we're really off the beaten path in terms of our server structure... (we use coffeescript + a structure based on https://github.com/JeyDotC/articles/blob/master/EXPRESS%20WITH%20SEQUELIZE.md, wrapped in feathersjs :) |
You can just do it in the style of a unit test, https://github.com/sequelize/sequelize/blob/master/test/dao/values.test.js#L199 |
.values/.toJSON is used nowhere in the lib code, hmm. |
Mind turning on long stack traces and giving me the stracktraces from your original example? I'd like to follow it :) |
Sure, I've pulled one of the examples & am modifying it, will dump a long stack trace... But your previous comment makes sense & might explain something. We are hitting the ORM via feathersjs wrappers, which is what's calling the toJSON (it automatically exposes JSON endpoints). You have an overload of .toJSON at line 623 of dao.js. So the flow is something like:
My best guess now is that if I hydrate and object and dump "toJSON" on it I can repro this without needing feathersjs, etc. |
Updating - my small test case did not repro the bug, and since this is now about feathersjs wrapping ORM the burden's on me to prove that the bug is actually happening inside your code :) so I'm looking at the big app again & tracing to see if maybe I'm sending back the wrong object to feathers. |
OK. Got it. Here's the repro case. Relevant parts are:
Am I doing something wrong by expecting to be able to use the object this way after .save()?
|
.save() should definitely not interfer with the ability to use .toJSON()/.get(). |
Investigating, more complex than i thought. |
@SteveEisner what dialect are you using? |
Solved it, postgres is doing some value processing on query result and uses object[key] directly instead of dataValues causing the setter to be triggered. |
Fix for #1234 - Postgres query should set on .dataValues instead of object
Fixed in #1239 |
Glad you found it and thanks for the fast response! I'm using the mysql dialect so I was wondering if a fix in postgres code would make the difference. But I've confirmed that this is fixed in both the test case & my larger app. |
Shouldn't ever have been an issue with the mysql dialect honestly. Test was passing for mysql :) |
I'm tracking a bug in our library that uses Sequelize, when updated to 1.7.0-beta8 (but possibly introduced in an earlier version)
We've defined a model with a type that uses a custom getter in the new format:
What we're seeing is that the getter is called twice - one time with the raw data from the database, and a 2nd time where the raw data is already JSON parsed. The 2nd time crashes because it receives a "hydrated" object instead of the raw string.
Stack trace of the first time:
and the 2nd:
notes -
The text was updated successfully, but these errors were encountered: