balderdashy / balderdashy/sails
Waterline allows NaN as a `number` field if set in beforeCreate() callback
- Dominant language
- JavaScript
- Stars
- 22.8k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
**Node version**: 10
**Sails version** _(sails)_: 1.2.3
# Observed
In interesting contrast to https://github.com/balderdashy/sails/issues/6868, `NaN` can be assigned to number fields in the `beforeCreate()` callback:
# Steps to recreate:
## Model
`api/models/Thing.js`:
```
module.exports = {
attributes: {
id: { unique:true, type:'number', required:true },
numVal: { type:'number' },
},
beforeCreate: (vals, callback) => {
vals.numVal = NaN;
callback();
},
};
```
## Sails console
```
> Thing.create({ id:32494 }).then(console.log).catch(console.error)
sails> Thing.find({}).then(console.log)
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
sails>
Warning: After transforming columnNames back to attribute names for model `thing`,
a record in the result has a value with an unexpected data type for property `numVal`.
The corresponding attribute declares `type: 'number'` but instead
of that, the actual value is:
NaN
> You are seeing this warning because there are records in your database that don't
> match up with your models. This is often the result of a model definition being
> changed without also migrating leftover data. But it could also be because records
> were added or modified in your database from somewhere outside of Sails/Waterline
> (e.g. phpmyadmin, or another app). In either case, to make this warning go away,
> you have a few options. First of all, you could change your model definition so
> that it matches the existing records in your database. Or you could update/destroy
> the old records in your database; either by hand, or using a migration script.
>
> (For example, to wipe all data, you might just use `migrate: drop`.)
>
> More rarely, this warning could mean there is a bug in the adapter itself. If you
> believe that is the case, then please contact the maintainer of this adapter by opening
> an issue, or visit http://sailsjs.com/support for help.
[ { createdAt: 1573548564619,
updatedAt: 1573548564619,
id: 32494,
numVal: NaN } ]
```
# Expected
Perhaps this _is_ the expected behaviour?
Contributor guide
Research direction
Start with the reproduction in api/models/Thing.js and run the supplied Sails console commands using the beforeCreate callback. Compare this behavior with issue 6868 and trace how the number field is handled after the callback; done means the expected treatment of NaN is decided and verified for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 38/100