balderdashy / balderdashy/sails
Incorrect warning reported when parent and child models share attributes of the same name, and constraint in child is violated.
- Dominant language
- JavaScript
- Stars
- 22.8k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
Incorrect warning reported when parent and child models share attributes of the same name, and constraint in child is violated.
I have not yet been able to recreate this in a simple project, but the basic model setup is:
```
//api/model/Myparent.js
module.exports = {
attributes: {
id: { ... },
mychild: {
model: 'Mychild',
},
some_email: {
type: 'string',
required: true,
isEmail: true,
},
},
};
```
```
//api/model/Mychild.js
module.exports = {
attributes: {
id: { ... },
some_email: {
type: 'string',
defaultsTo: '',
},
},
};
```
If you have an instance of `Mychild` with `some_email` set as `null` in the DB, and an instance of `Myparent` which references the `Mychild` instance, a query such as:
```
MyParent
.find()
.populate('mychild');
```
will throw an error like so:
> Warning: After transforming columnNames back to attribute names for model `myparent`, a record in the result has a value of `null` for property `some_email`. Since the `some_email` attribute declares `type: 'string'`, without ALSO declaring `allowNull: true`, this `null` value is unexpected. (To resolve, either change this attribute to `allowNull: true` or update existing records in the database accordingly.) 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. 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.
However, the error should actually be reported against the child model.
cc @okwonks
Contributor guide
Research direction
Start with the Myparent.js and Mychild.js model definitions and the MyParent.find().populate('mychild') query described in the report. Try to reproduce the warning with a null child some_email value, then trace how populated records are mapped back to model attributes. Done means the warning identifies the child model rather than the parent model.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100