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.

How to setup relations between tables ?

See original GitHub issue

Hi guys,

I’m testing an app with Knex right now, and having a bit of a problem when preparing my schemas. Since app.configure doesn’t wait to be finished to go on the next one, i’m getting “relation does not exist” errors half of the time when loading with my empty database.

I’m using the app structure from the generator for postgres (which is loaded with sequelize by default). So far, i got this :

// services/index.js

const knex = require( 'knex' );
const authentication = require( './authentication' );
const user = require( './user' );
const client = require( './client' );

module.exports = function () {
    const app = this;

    const knex_connection = knex( {
        client: 'pg',
        connection: app.get( 'postgres' )
    } );
    app.set( 'knex', knex_connection );

    app.configure( authentication );
    app.configure( user );
    app.configure( client );

}
// services/user/index.js

const service = require( 'feathers-knex' );
const user = require( './user-model' );
const hooks = require( './hooks' );
const auth = require( '../../filters/auth' );

module.exports = function () {
    const app = this;

    return user( app.get( 'knex' ) ).then( () => {

        const options = {
            Model: app.get( 'knex' ),
            name: 'users',
            paginate: {
                default: 1,
                max: 10
            }
        };

        // Initialize our service with any options it requires
        app.use( '/users', service( options ) );

        const userService = app.service( '/users' );
        userService.before( hooks.before );
        userService.after( hooks.after );
        userService.filter( [ auth.isAuthenticated(), auth.isOwner() ] );

    } );

};
// services/user/user-model.js

module.exports = function ( knex ) {

    const schema = function() {
         return knex.schema.createTable( 'users', table => {

            table.increments( 'id' ).primary();
            table.string( 'email' ).notNullable().unique();
            table.string( 'password' ).notNullable();
            table.specificType( 'roles', 'varchar(255)[]' ).notNullable();
            table.timestamp( 'created_at' ).notNullable().defaultTo( knex.raw( 'now()' ) );
            table.timestamp( 'updated_at' ).notNullable().defaultTo( knex.raw( 'now()' ) );

        } );
    }

    return knex.schema.hasTable( 'users' ).then( exists => !exists && schema() );

};

Is it possible to make app.configure wait or i am doing it the wrong way ?

Thanks.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
jayalfredprufrockcommented, Aug 2, 2016

Unfortunately I’m only using this approach on closed-source projects at the moment, but I’m really not doing anything special. I’m just utilizing the knex CLI to build and seed my database before testing and deploying. See http://knexjs.org/#Migrations-CLI

My knexfile looks something like this and is in a top level directory “db”:

module.exports = {

  development: {
    client: 'sqlite3',
    connection: {
      filename: __dirname + '/development.sqlite3'
    },
    seeds: {
      directory: './seeds'
    },
    migrations: {
      tableName: 'migrations',
      directory: './migrations'
    },
    useNullAsDefault: true
  }
}

Then I connect everything with some scripts:

  "scripts": {
    "test": "npm run seed-db && npm run build-server && NODE_ENV=development tape -r babel-register \"test/**/*.js\" | tap-difflet",
    "develop": "npm run build-server && NODE_ENV=development node dist/",
    "start": "NODE_ENV=production npm run build-server && npm run build-client && NODE_ENV=production node dist/",
    "build-server": "babel --plugins transform-runtime -d dist/ src/",
    "build-client": "NODE_ENV=production webpack -p --progress --colors",
    "build-db": "rimraf db/development.sqlite3 && npm run knex -- migrate:latest",
    "seed-db": "npm run knex -- seed:run",
    "knex": "knex --knexfile='db/knexfile.js'"
  }

Hope that helps!

0reactions
ejmudrakcommented, Nov 9, 2021

@eric-burel @daffl Did you ever happen to find a solution to this issue? I’m having having issues with async configuration of knex models with relations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create, edit or delete a relationship - Microsoft Support
Drag a field (typically the primary key) from one table to the common field (the foreign key) in the other table. To drag...
Read more >
Access - How to Create a Relationship Between Two Tables
Access 2016 - Relationships - How To Create One To Many Relationship in Database Between Two Tables · Access: Formatting Forms · Access...
Read more >
Access 2016 - Relationships - How To Create One To Many ...
Key moments. View all · create a relationship between the two tables · create a relationship between the two tables · create a...
Read more >
Video: Create table relationships (Access basics, part 2)
Creating relationships between your tables (5:07) An introduction to relationships and the 3 types of relationships. ; Create a relationship with the Lookup ......
Read more >
Microsoft Access Relationships: Relating Multiple ... - YouTube
Microsoft Access Relationships : Relating Multiple Tables, Relational Data, ... I'm in the process of setting up my first Access database.
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