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.

hasTable() doesn't resolve with a boolean

See original GitHub issue

Hi, I am using knex schema builder to create the database tables

// create categories table
db.hasTable('categories').then(function (exists) {
  debug(exists)
  if ( exists ) return

  return db.createTable('categories', function (table) {
    table.increments('id')
    table.string('name').notNullable()
    table.string('slug').notNullable().index()
  })
}).then(function () { debug("Table `categories` created") })

// create tags table
db.hasTable('tags').then(function (exists) {
  debug(exists)
  if ( exists ) return

  return db.createTable('tags', function (table) {
    table.increments('id')
    table.string('name').notNullable()
    table.string('slug').notNullable().index()
  })
}).then(function () { debug("Table `tags` created") })

after debugging, exists is an array instead of a boolean, below the output of the console

database [ true, false ] +0ms      <= 
database [ true, false ] +4ms
database Table `categories` created +2ms
database Table `tags` created +0ms

true means that the table categories already exist, tags does not.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
wubzzcommented, Mar 24, 2017

I’m pretty sure this is caused by reusing the schema reference rather than instantianting a new reference each time.

For example this:

const schema = knex.schema;

return schema.hasTable('test') //Runs hasTable test
.then(console.log)// true
.then(() => schema.hasTable('test2')) //Runs hasTable test AND hasTable test2
.then(console.log); //[true, false] -- Since two queries run

The schema reference is still just a query builder. So everytime .then is called, it will run the queries from the builder chain.

Doing this should solve the ‘strangeness’:

return knex.schema.hasTable('test') //Runs hasTable test
.then(console.log)// true
.then(() => knex.schema.hasTable('test2')) //Runs hasTable test2
.then(console.log); //false

Or even better:

const schema = knex.schema;

return schema.hasTable('test')
.then(() => schema.hasTable('test2'))
.spread((hasTableTest, hasTableTest2) => {...})

I wouldn’t consider this a bug. @elhigu What do you think?

CC @tgriesser @jurko-gospodnetic

1reaction
pirannacommented, Aug 3, 2022

I stumbled with the same issue during the last 2 hours, I find It good as an optimización but should be shown in the docs. Better, has two methods, hasTable() that always returns a Booleana, and hasTables() that always returns an Array of Booleana

Read more comments on GitHub >

github_iconTop Results From Across the Web

hasTable() doesn't resolve with a boolean · Issue #1509 · knex ...
Hi, I am using knex schema builder to create the database tables // create categories table db.hasTable('categories').then(function (exists) ...
Read more >
hash table does not count values correctly - Stack Overflow
Here, i'm trying to implement a chaining hash table. In here when I enter a same key, it joins with existing key and...
Read more >
Java Hashtable class - HowToDoInJava
Java Hashtable class is an implementation of hash table data structure. ... boolean isEmpty() : It returns true if the hashtable is empty; ......
Read more >
Everything you wanted to know about hashtables - PowerShell
A hashtable is a data structure, much like an array, except you store each value (object) using a key. It's a basic key/value...
Read more >
An Introduction to java.util.Hashtable Class - Baeldung
A guide to understanding the Hashtable structure in Java. ... Any Java object inherits the hashCode() method which returns an int value.
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