question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

migrate:make foo results in "calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.

See original GitHub issue

Environment

Knex version: 0.21.0 Database + version: PostgreSQL 12.2 (docker) OS: Ubuntu 19.10

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do

When trying to create migration with:

npx knex --knexfile ./src/db/knexfile.js migrate:make foobar

knex complains about:

calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.

The same happens when I try seed:make foobar but I’m able to run queries against the db.

I’m using node experimental esm support via "type": "module". To keep knex happy I’m employing “a non-module package.json” as described by @D10221 in this comment.

  1. Error message
Working directory changed to ~/code/playground/moviedb/src/db
calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.
(node:18834) UnhandledPromiseRejectionWarning: error: select * - SELECT * with no tables specified is not valid
    at Connection.parseE (/home/boojum/code/playground/moviedb/node_modules/pg/lib/connection.js:600:48)
    at Connection.parseMessage (/home/boojum/code/playground/moviedb/node_modules/pg/lib/connection.js:399:19)
    at Socket.<anonymous> (/home/boojum/code/playground/moviedb/node_modules/pg/lib/connection.js:115:22)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:273:9)
    at Socket.Readable.push (_stream_readable.js:214:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
(node:18834) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:18834) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  1. Reduced test code, for example in https://npm.runkit.com/knex or if it needs real database connection to MySQL or PostgreSQL, then single file example which initializes needed data and demonstrates the problem.

Repo with toy code demonstrating the isse can be found on gitlab. I’m betting on issue being with knexfile which can be seen below:

const dotenv = require("dotenv").config({ path: __dirname + "/../../.env" });
const knex = require("knex");

const config = {
  development: {
    client: "pg",
    connection: {
      host: process.env.POSTGRES_HOST,
      port: process.env.POSTGRES_PORT,
      user: process.env.POSTGRES_USER,
      password: process.env.POSTGRES_PASSWORD,
      database: process.env.POSTGRES_DB,
    },
    migrations: {
      tableName: "knex_migrations",
      directory: "./migrations",
    },
    seeds: { directory: "./seeds" },
  },
};

const db = knex(config[`${process.env.NODE_ENV}`]);
module.exports = knex(config[`${process.env.NODE_ENV}`]);

module.exports = db;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
D10221commented, Apr 20, 2020

It seems to me the problem is here

const db = knex(config[`${process.env.NODE_ENV}`]); // ?
module.exports = knex(config[`${process.env.NODE_ENV}`]); // exporting Knex instead of Config

‘module.exports’ is ‘Knex Function’, instead of “Knex’s Config”

/*  It works */
module.exports = config[`${process.env.NODE_ENV}`];

I think… you could share your ‘knex’ configuration between the ‘migrations’ and the app code. instead of the Knex instance

5reactions
nexusbrcommented, Jul 8, 2020

Thanks @D10221

knexfile.js

var options = {
    development: {
        client: 'pg',
        connection: {
            host: process.env.SERVIDOR_MAPAS_ENDERECO,
            user: process.env.SERVIDOR_MAPAS_USUARIO,
            password: process.env.SERVIDOR_MAPAS_SENHA,
            database: process.env.SERVIDOR_MAPAS_NOME_BANCO_DADOS,
            port: process.env.SERVIDOR_MAPAS_PORTA
        }
    },
    pool: {
        min: 2,
        max: 10
    },
    migrations: {
        tableName: 'knex_migrations'
    },
    production: {
        ...
        ...
    },
};

var environment = process.env.NODE_ENV || 'development';
var config = options[environment];
module.exports = config;

database.js

var options = require('./config/knexfile');
module.exports = require('knex')(options);

and at file_to_access_data.js

var knex = require('../../database');
knex.raw('insert into ...)
Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql - Knex.js query throws warning "Calling knex without a ...
Knex :warning - calling knex without a tableName is deprecated. Use knex.queryBuilder() instead. Please review following error and my codes.
Read more >
Knex Query Builder
The query builder starts off either by specifying a tableName you wish to query against, or by calling any method directly on the...
Read more >
Test Driven Development with Node, Postgres, and Knex (Red ...
Knex is a SQL query builder that we can use with PostgreSQL to handle migrations, ... Deletes ALL existing entries knex('table_name').del(), ...
Read more >
Knex cheatsheet - Devhints
One-page guide to Knex: usage, examples, and more. Knex is an SQL query builder for Node.js.This guide targets v0.13.0.
Read more >
Other Methods | Objection.js - GitHub Pages
toKnexQuery(). knexQueryBuilder = queryBuilder.toKnexQuery();. Compiles the query into a knex query and returns the knex query builder instance.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found