join-monster icon indicating copy to clipboard operation
join-monster copied to clipboard

PG strange error: doesn't retrieve columns, but they are there!

Open frederikhors opened this issue 7 years ago • 4 comments

I have a problem this with Postgres (PG dialect). But this is really strange to me.

ENTIRE REPRODUCTION HERE (also with Postgres database online): https://glitch.com/edit/#!/postgres-join-monster

If you click on "Show Live" button it opens Graphql Playground.

The schema, resolvers and query is the same as the Readme here: https://github.com/acarl005/join-monster-graphql-tools-adapter

Can you see the error in Playground?

When I query with:

query {
  user(id: 1) {
    id
    fullName
    email
    posts {
      id
      body
      numComments
      comments {
        id
        body
        userId
      }
    }
  }
}

it says:

{
  "data": {
    "user": null
  },
  "errors": [
    {
      "message": "column posts.userid does not exist",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "user"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "name": "SequelizeDatabaseError",
          "parent": {
            "name": "error",
            "length": 104,
            "severity": "ERROR",
            "code": "42703",
            "position": "317",
            "file": "parse_relation.c",
            "line": "2876",
            "routine": "errorMissingColumn",
            "sql": "SELECT\n  \"user\".\"id\" AS \"id\",\n  \"user\".\"email\" AS \"email\",\n  \"posts\".\"id\" AS \"posts__id\",\n  (SELECT count(*) FROM comments where \"posts\".id = comments.postId) AS \"posts__numComments\",\n  \"user\".\"first_name\" AS \"first_name\",\n  \"user\".\"last_name\" AS \"last_name\"\nFROM users \"user\"\nLEFT JOIN posts \"posts\" ON \"user\".id = \"posts\".userId\nWHERE \"user\".id = 1"
          },
          "original": {
            "name": "error",
            "length": 104,
            "severity": "ERROR",
            "code": "42703",
            "position": "317",
            "file": "parse_relation.c",
            "line": "2876",
            "routine": "errorMissingColumn",
            "sql": "SELECT\n  \"user\".\"id\" AS \"id\",\n  \"user\".\"email\" AS \"email\",\n  \"posts\".\"id\" AS \"posts__id\",\n  (SELECT count(*) FROM comments where \"posts\".id = comments.postId) AS \"posts__numComments\",\n  \"user\".\"first_name\" AS \"first_name\",\n  \"user\".\"last_name\" AS \"last_name\"\nFROM users \"user\"\nLEFT JOIN posts \"posts\" ON \"user\".id = \"posts\".userId\nWHERE \"user\".id = 1"
          },
          "sql": "SELECT\n  \"user\".\"id\" AS \"id\",\n  \"user\".\"email\" AS \"email\",\n  \"posts\".\"id\" AS \"posts__id\",\n  (SELECT count(*) FROM comments where \"posts\".id = comments.postId) AS \"posts__numComments\",\n  \"user\".\"first_name\" AS \"first_name\",\n  \"user\".\"last_name\" AS \"last_name\"\nFROM users \"user\"\nLEFT JOIN posts \"posts\" ON \"user\".id = \"posts\".userId\nWHERE \"user\".id = 1",
          "stacktrace": [
            "SequelizeDatabaseError: column posts.userid does not exist",
            "    at Query.formatError (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/sequelize/4.38.0/node_modules/sequelize/lib/dialects/postgres/query.js:363:16)",
            "    at query.catch.err (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/sequelize/4.38.0/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)",
            "    at tryCatcher (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/util.js:16:23)",
            "    at Promise._settlePromiseFromHandler (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/promise.js:512:31)",
            "    at Promise._settlePromise (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/promise.js:569:18)",
            "    at Promise._settlePromise0 (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/promise.js:614:10)",
            "    at Promise._settlePromises (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/promise.js:690:18)",
            "    at _drainQueueStep (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/async.js:138:12)",
            "    at _drainQueue (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/async.js:131:9)",
            "    at Async._drainQueues (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/async.js:147:5)",
            "    at Immediate.Async.drainQueues (/rbd/pnpm-volume/13dab6e7-982a-4e75-a7d2-4720db53e6c6/node_modules/.registry.npmjs.org/bluebird/3.5.2/node_modules/bluebird/js/release/async.js:17:14)",
            "    at runCallback (timers.js:810:20)",
            "    at tryOnImmediate (timers.js:768:5)",
            "    at processImmediate [as _immediateCallback] (timers.js:745:5)"
          ]
        }
      }
    }
  ]
}

If I change it from:

sqlJoin: (userTable, postTable) => ${userTable}.id = ${postTable}.userId,

to:

sqlJoin: (userTable, postTable) => ${userTable}.id = ${postTable}."userId",

it works. But there is another error after...

Where am I wrong?

frederikhors avatar Sep 05 '18 12:09 frederikhors

Maybe I simply need to use "userId" instead of userId.

frederikhors avatar Sep 05 '18 12:09 frederikhors

I got a feeling Sequelize names the PSQL columns in snake_case, while you build the query with camelCase, it is just a guess though.

Can you print your table information from pqsl directly? When logged in to the psql console, just type \d posts or \d users.

If this is it, then you should construct the join with the snake-cased column name: sqlJoin: (userTable, postTable) => ${userTable}.id = ${postTable}.user_id

wtho avatar Sep 09 '18 10:09 wtho

@wtho I have table names like:

  • posts.UserId
  • users

frederikhors avatar Sep 09 '18 17:09 frederikhors

Of course you have to use "userId" in Postgres. Let's close this.

melounek avatar Mar 25 '20 21:03 melounek