How to setup relations between tables ?
See original GitHub issueHi 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:
- Created 7 years ago
- Comments:9 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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”:
Then I connect everything with some scripts:
Hope that helps!
@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.