Skip to content
Snippets Groups Projects
Commit 617284d1 authored by Federico Massaioli's avatar Federico Massaioli
Browse files

fix: capitalized table names on join().update() and join().delete()

parent 869eede6
No related branches found
No related tags found
1 merge request!801fix: capitalized table names on join().update() and join().delete()
Pipeline #740877046 passed
......@@ -37,7 +37,7 @@ Delete.prototype.format = function () {
this.predicate = `${target.on.predicates} AND (${this.predicate}) `;
sql += `USING ${target.relation} `;
sql += `USING ${target.target} `;
sql += _.tail(this.source.joins).map(j => `${j.type} JOIN ${j.target} ON ${j.on.predicates} `).join('');
sql += `WHERE ${this.predicate} `;
sql += `RETURNING ${this.source.delimitedFullName}.*`;
......
......@@ -60,7 +60,7 @@ Update.prototype.format = function () {
this.predicate = `${target.on.predicates} AND (${this.predicate}) `;
sql += `FROM ${target.relation} `;
sql += `FROM ${target.target} `;
sql += _.tail(this.source.joins).map(j => `${j.type} JOIN ${j.target} ON ${j.on.predicates} `).join('');
sql += `WHERE ${this.predicate} `;
sql += `RETURNING ${this.source.delimitedFullName}.*`;
......
......@@ -14,6 +14,14 @@ CREATE TABLE beta (
FOREIGN KEY (alpha_id) REFERENCES alpha(id)
);
CREATE TABLE "CapitalizedBeta" (
id SERIAL NOT NULL PRIMARY KEY,
alpha_id INT,
val TEXT,
j JSONB,
FOREIGN KEY (alpha_id) REFERENCES alpha(id)
);
ALTER TABLE beta DROP COLUMN val2;
CREATE TABLE gamma (
......@@ -50,6 +58,15 @@ VALUES
(null, 'not four', null),
(null, 'not five', '{"x": {"y": "test"}}'::JSONB);
INSERT INTO "CapitalizedBeta" (alpha_id, val, j)
VALUES
(1, 'alpha one', null),
(2, 'alpha two', null),
(3, 'alpha three', null),
(3, 'alpha three again', null),
(null, 'not four', null),
(null, 'not five', '{"x": {"y": "test"}}'::JSONB);
INSERT INTO gamma (alpha_id_one, alpha_id_two, beta_id, val, j)
VALUES
(1, 1, 1, 'alpha one alpha one beta one', null),
......
......@@ -1357,6 +1357,25 @@ describe('join', function () {
});
});
it('updates the origin of a table-only join with capitals in the joined table', function () {
return db.alpha.join({
beta: {
type: 'INNER',
relation: 'CapitalizedBeta',
on: {alpha_id: 'id'}
}
}).update({
'beta.id': 3
}, {
val: 'Something Else'
}).then(result => {
assert.deepEqual(result, [{
id: 3,
val: 'Something Else'
}]);
});
});
it.skip('updates a compound readable with constant criteria', function () {
// TODO offset needs to be stored between join and where for this to work
return db.alpha.join({
......@@ -1477,6 +1496,20 @@ describe('join', function () {
});
});
it('delete-joins', function () {
return db.gamma.join({
beta: {
relation: 'CapitalizedBeta',
alpha: {},
on: {id: 'beta_id'}
}
}).destroy({
'alpha.id': 3
}).then(result => {
assert.deepEqual(result, []);
});
});
it('runtime errors on out-of-bounds references to the FROM table after USING', async () => {
let err;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment