Skip to content
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

Invalid column name errors #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions core/required/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,11 @@ class Composer {

if (rel) {

// if it's not found, return null...
if (!rel.getModel().hasColumn(column[0])) {
return null;
let model = rel.getModel();

// block out bad column names
if (!model.hasColumn(column[0])) {
throw new Error(`Column '${column[0]}' doesn't exist on '${model.name}'`);
}

table = rel.getModel().table();
Expand All @@ -608,7 +610,7 @@ class Composer {

// block out bad column names
if (!rel && !Model.hasColumn(columnName)) {
return null;
throw new Error(`Column '${columnName}' doesn't exist on '${this.Model.name}'`);
}

let value = comparisons[comparison];
Expand Down
52 changes: 48 additions & 4 deletions test/tests/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = Nodal => {

const async = require('async');

let expect = require('chai').expect;
const { assert, expect } = require('chai');

describe('Nodal.Composer', function() {

Expand Down Expand Up @@ -1225,23 +1225,23 @@ module.exports = Nodal => {
children__is_favorite: true,
children__license: null,
pets__name: 'Oliver',
pets__alive: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write a new test case here instead of adapting an old one

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually an issue with the existing tests since the column active doesn't exist (it's is_active). I had to update them because my new changes would break the tests otherwise. This column doesn't seem to affect the result of the test anyway since they still pass like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah gotcha. Makes sense then

pets__is_alive: true,
name: 'Zoolander',
pets__animal__in: ['Cat']
},
{
children__is_favorite: true,
children__license__not_null: true,
pets__name: 'Oliver',
pets__alive: true,
pets__is_alive: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for comparators too (pets__name__ilike or __lt)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I could add those if you like but this change was just to fix the bad column name used in the original test.

name: 'Zoolander',
pets__animal__in: ['Cat']
},
{
careers__title: 'Freelancer',
careers__is_active: true,
pets__name: 'Oliver',
pets__alive: true,
pets__is_alive: true,
name: 'Zoolander',
pets__animal__in: ['Cat']
},
Expand Down Expand Up @@ -1709,6 +1709,50 @@ module.exports = Nodal => {

});

it('Should throw errors when querying with a bad column name', done => {

try {

Parent.query()
.join('pets')
.where({ active: true })
.end();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the style consistent with other tests (use the callback and catch the error)

Copy link
Author

@kangabru kangabru Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I wrote the code is such that it throws an error outside of the end() call. I did this is because it's more of a developer error, not a DB one. If I did it inside the end call then it might be less obvious.

Would you prefer me to have it throw the error inside the end() call?


assert.fail("Expected query to throw an error");

} catch (error) {

expect(error.message).to.contain("Column 'active'");
expect(error.message).to.contain("'Parent'");

}

done();

});

it('Should throw errors when querying joins with a bad column name', done => {

try {

Parent.query()
.join('pets')
.where({ pets__active: true })
.end();

assert.fail("Expected query to throw an error");

} catch (error) {

expect(error.message).to.contain("Column 'active'");
expect(error.message).to.contain("'Pet'");

}

done();

});

// IMPORTANT: Do npt place any tests after the `Should do a destroy cascade`
// test since all models will be gone

Expand Down