3

I'm developing a nodejs project with a postgresql-db. I've created a schema for the db with some tables and now I've created a file with some prepared data (multilple rows) to insert. For that I followed the instructions of this entry: Multi-row insert with pg-promise.

The problem is, that I get following error: { error: relation "tenantx.ObjectGroup" does not exist ... but it does. I can also see that from my api, when I want to read the table content, it returns an empty object. Can the "schema.table"-declaration be the reason?

The code for the prepared data looks like this:

    'use strict';
     const pgp = require('pg-promise')({
                // Initialization Options
            });

        const csObjectGroup = new pgp.helpers.ColumnSet(['objectgroup', 'description'], {
                    table: 'tenantx.ObjectGroup'
                });
                // data input values:
                const valuesObjectGroup = [{
                    objectgroup: 'bla',
                    description: 'bla'
                }, {
                    objectgroup: 'blu',
                    description: 'blu'
                }, {
                    objectgroup: 'bla',
                    description: 'bla'
                }];
module.exports = {
    csObjectGroup: csObjectGroup,
    valuesObjectGroup: valuesObjectGroup
}

And the code which is called from api to setup content: // insert prepared data into tables

function initializeData(pTenantId, dbUri) {
    var dbPostgres = dbUri;
    const datafile = require("./../Data/data_" + pTenantId);

    var statements = [];

    var valuesObjectGroup = datafile.valuesObjectGroup;
    var csObjectGroup = datafile.csObjectGroup;

    statements.push(valuesObjectGroup, csObjectGroup);

    var i, query;
    for (i = 0; i < statements.length - 1; i += 2) {
        query = pgp.helpers.insert(statements[i], statements[i + 1]);
        dbPostgres.none(query)
            .then(data => {
                console.log("+++++ Data successfully initialized.");
            })
            .catch(err => {
                console.log("----- Data could not be initialized.");
                console.log(err);
            });
    }
    return true;
}

I've created the table from this:

const createObjectGroupTable =
    'CREATE TABLE IF NOT EXISTS tenantx.ObjectGroup \
            ( \
                id serial, \
                objectgroup varchar(50), \
                description varchar(100), \
                PRIMARY KEY (id) \
            )';
Galadriel
  • 359
  • 5
  • 20

1 Answers1

3

If you look at the reported error with more attention:

{ error: relation "tenantx.ObjectGroup" does not exist ...

That's not "tenantx"."ObjectGroup", that's just one table name "tenantx.ObjectGroup".

And the problem originates from the way you declare the table:

const csObjectGroup = new pgp.helpers.ColumnSet(['objectgroup', 'description'], {
                    table: 'tenantx.ObjectGroup'
                });

instead of providing schema + table names, you specify it all as a table name, and so it is escaped accordingly.

The correct way to specify schema + table can be done in one of the following ways:

table: {schema: 'tenantx', table: 'ObjectGroup'}

or:

table: new pgp.helpers.TableName('ObjectGroup', 'tenantx')

or:

table: new pgp.helpers.TableName({table: 'ObjectGroup', schema: 'tenantx'})

See API: TableName.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • I've tried now all three possibilities, but I get always the same error. I also logged the tablename and it seems to be right concanated: "tenantx"."ObjectGroup". Can there be another reason? The log says: `***** "tenantx"."ObjectGroup" ----- Data could not be initialized. { error: relation "tenantx.ObjectGroup" does not exist [...]` – Galadriel May 07 '19 at 06:55
  • Logs with logged columnset say: `***** ColumnSet { table: "tenantx"."ObjectGroup" columns: [ Column { name: "objectgroup" } Column { name: "description" } ] } ----- Data could not be initialized. { error: relation "tenantx.Object" does not exist` – Galadriel May 07 '19 at 07:03
  • One difference is, that my pgp.helpers.insert(...) is a var and no const because of the for loop. Can it be a reason? – Galadriel May 07 '19 at 07:10
  • First, it was `tenantx.ObjectGroup`, now it is `tenantx.Object`. I don't know what you really did there, I cannot see the code, but something isn't right on your side. The solution I gave you must fix exactly the error you showed. – vitaly-t May 07 '19 at 15:21
  • Those are both tables as I have more than only one. Probably I have copied the log not in the right order, I'm sorry. Well, this is weird.. – Galadriel May 07 '19 at 15:56