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.

Knex seed for postgres

See original GitHub issue

I was playing around with different databases for this kit since we are mentioning over here that any database can be plugged into the kit in the same way as SQLite.

So, I tried plugging postgres and faced an issue with the knex seed provided in this kit. The problem is we are using truncate() for seed migration and comment table is referencing post table to populate the comments for a particular post_id.

Since postgres does not allow truncating tables if the table has any foreign key reference making the script yarn seed fails.

screen shot 2017-05-17 at 15 00 54

However, using raw queries from knex does push the seed to completion, but it does not insert the post_id reference in comment table.

From this (in seeds/post.js)

await Promise.all([
    knex('post').truncate(),
    knex('comment').truncate()
]);

to this

await Promise.all([
    knex.raw('TRUNCATE TABLE post CASCADE'),
    knex.raw('TRUNCATE TABLE comment CASCADE')
]);

However, it returns null for post_id

screen shot 2017-05-17 at 15 05 31

This is the final seed function in case of postgres

export async function seed(knex, Promise) {
  await Promise.all([
    knex.raw('TRUNCATE TABLE post CASCADE'),
    knex.raw('TRUNCATE TABLE comment CASCADE')
  ]);

  await Promise.all([...Array(20).keys()].map(async ii => {
    const post = await knex('post').insert({
      title: `Post title ${ii + 1}`,
      content: `Post content ${ii + 1}`
    });

    await Promise.all([...Array(2).keys()].map(async jj => {
      return knex('comment').insert({
        post_id: post[0],
        content: `Comment title ${jj + 1} for post ${post[0]}`
      });
    }));
  }));
}

P.S -> Just in case you want to see my modified knexdata.js file

export const development = {
  client: 'pg',
  debug: true,
  connection: {
    host: '127.0.0.1',
    user: 'postgres',
    password: '*****',
    database: 'dev_db'
  },
  seeds: {
    directory: './src/server/database/seeds'
  },
  migrations: {
    directory: './src/server/database/migrations'
  },
  useNullAsDefault: true
};

Another problem is the primary key (autoincrement) for post and comment table is not resetting back to 1 after running the seed every time.

screen shot 2017-05-17 at 15 16 20

Is there any workaround for this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mairhcommented, May 18, 2017
1reaction
larixercommented, May 18, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

Knex Migration — For schema and seeds with PostgreSQL
Knex -migrate will keep track of our schema changes and dummy data. It is a very useful tool while migrating database. Knex-migrate will...
Read more >
Seed Knex PostgreSQL Database with JSON Data - Medium
We'll go over how to integrate Knex with a PostgreSQL database and how to seed the database with JSON-formatted data.
Read more >
Migration and seeding instructions using Knex.js! - gists · GitHub
Seeding Your Database. Similar to migrations, the knex module allows us to create scripts to insert initial data into our tables called seed...
Read more >
Knex.js: SQL Query Builder for Javascript
Knex.js is a "batteries included" SQL query builder for PostgreSQL, CockroachDB, MSSQL, MySQL, MariaDB, SQLite3, Better-SQLite3, Oracle, and Amazon Redshift ...
Read more >
Database seeding with Knex - DEV Community ‍ ‍
Upon running the command, Knex will create a file and pre-populate it with some boilerplate code to get us started: exports.seed = function(knex) ......
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