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

TypeError: Invalid column syntax: "source-layer". #494

Closed
stepankuzmin opened this issue Mar 14, 2018 · 9 comments
Closed

TypeError: Invalid column syntax: "source-layer". #494

stepankuzmin opened this issue Mar 14, 2018 · 9 comments
Labels

Comments

@stepankuzmin
Copy link

It seems that pg-promise doesn't support column names with - sign.

Expected behavior

ColumnSet {
    columns: [
        Column {
            name: "source-layer"
        }
    ]
}

Actual behavior

TypeError: Invalid column syntax: "source-layer".
    at parseColumn (/Users/stepan/projects/protoproto/node_modules/pg-promise/lib/helpers/column.js:279:11)
    at new Column (/Users/stepan/projects/protoproto/node_modules/pg-promise/lib/helpers/column.js:136:22)
    at ColumnSet.columns.columns.map.c (/Users/stepan/projects/protoproto/node_modules/pg-promise/lib/helpers/columnSet.js:213:57)
    at Array.map (<anonymous>)
    at new ColumnSet (/Users/stepan/projects/protoproto/node_modules/pg-promise/lib/helpers/columnSet.js:212:32)

Steps to reproduce

const pgp = require('pg-promise')();
new pgp.helpers.ColumnSet(['source-layer']);

Environment

  • Version of pg-promise: 8.2.2
  • Version of Node.js: 9.7.1

P.S. I've tried different modifiers with

new pgp.helpers.Column({ name: '"source-layer"', mod: ':raw' })

but it didn't succeeded.

@vitaly-t
Copy link
Owner

vitaly-t commented Mar 14, 2018

This is by design, see the API: http://vitaly-t.github.io/pg-promise/helpers.Column.html

Destination column name + source property name (if prop is skipped). The name must adhere to JavaScript syntax for variables, unless prop is specified, in which case name represents only the column name, and therefore can be any string.

@stepankuzmin
Copy link
Author

So there is no way to use helpers with this kind of columns then?

@vitaly-t
Copy link
Owner

Are you using string presentation for your source property names? If not, then what is your source property name?

@stepankuzmin
Copy link
Author

stepankuzmin commented Mar 14, 2018

I've "source-layer" text in my create table. I'm trying to use insert and update helpers on this table.

@vitaly-t
Copy link
Owner

I understand that, I asked about the name of the property in the source object that contains the value.

@stepankuzmin
Copy link
Author

stepankuzmin commented Mar 14, 2018

What do you mean by source object? Query result object? If yes, then it is source-layer too. Actually I'm refering to it as source-layer everywhere.

Yes, I'm using string presentation for property names.

@vitaly-t
Copy link
Owner

vitaly-t commented Mar 14, 2018

I'm using string presentation for property names.

If property names in your source object do not satisfy the open-variable syntax for JavaScript, then type Column cannot directly map to such properties.

This is because the whole idea of the helpers namespace is around preparing high-performance formatting templates that use Named Parameters, which in turn can only use open-variable syntax for properties.

What you are doing is unusual, and can only happen when trying to work-around some legacy bad written database. New databases should never use names like column-bla-name, that's a horrible idea. The standard is column_bla_name, which then can be auto-camelized with pg-promise, if you want.

If you still have such awkward columns to deal with, you would have to provide a value override, i.e. your column would be:

{name: 'column-layer', prop: 'unused', init: c => c.source['column-layer']}

i.e. you'd have to provide any prop just to indicate that name no longer represents the source property, only the column name, and thus can be any string, as explained in the API.

Just shorter alternative:

{name: 'column-layer', prop: '_', init: c => c.source['column-layer']}

@stepankuzmin
Copy link
Author

Thanks for the reply! That makes things clear.

@vitaly-t
Copy link
Owner

vitaly-t commented Jul 9, 2024

Related issue, with explanation - #937

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants