title: I'm having trouble with foreign-key in Knex.JS
body: <p>Im try to insert data on my database with Knex.JS, in some tables I have a foreign key, and I guess the trouble is occurring with this part of project.</p>
  <p>I'm having this error message:</p>
  <pre><code>error: insert into &quot;classes&quot; (&quot;cost&quot;, &quot;subject&quot;, &quot;user_id&quot;) values ($1, $2, DEFAULT) - null value in column &quot;user_id&quot; violates not-null constraint 
  </code></pre>
  <p>MY knexfile:</p>
  <pre><code>import {resolve} from 'path'
  
  module.exports = {
  
    development: {
      client: 'pg',
      connection: {
        database: &quot;XXXX&quot;,
        user: &quot;XXXX&quot;,
         password:&quot;XXXXXXX&quot;
  
      },
      migrations: {
        directory: resolve(__dirname, 'src', 'database', 'migrations')
      }
    },
    useNullAsDefault: true
  };
  </code></pre>
  <p>and the migrations im using:</p>
  <pre><code>import Knex from 'knex'
  
  export async function up(Knex: Knex) {
      return await Knex.schema.createTable('users', table =&gt; {
          table.increments('id').notNullable().primary();
          table.string('name').notNullable();
          table.string('avatar').notNullable();
          table.string('whatsapp').notNullable();
          table.string('bio').notNullable();
      })
  }
  
  
  export async function down(knex: Knex) {
      return await knex.schema.dropTable('users')
  }
  </code></pre>
  <blockquote>
  </blockquote>
  <pre><code>import Knex from 'knex'
  
  export async function up(Knex: Knex) {
      return await Knex.schema.createTable('classes', table =&gt; {
          table.increments('id').notNullable().primary().unique();
          table.string('subject').notNullable();
          table.decimal('cost').notNullable();
  
  
          table.integer('user_id').
          notNullable().
          references('id').
          inTable('users').
          onDelete('CASCADE').
          onUpdate('CASCADE')
      })
  }
  
  
  export async function down(knex: Knex) {
      return await knex.schema.dropTable('classes')
  }
  </code></pre>
  <p>I guess the trouble can occur i the moment of insert data to as some data depend of other data:</p>
  <pre><code>async  store(req: Request, res: Response) {
          const {name,
              avatar,
              whatsapp,
              bio,
              subject,
              cost,
              schedule
              } = req.body
      
              const trx = await knex.transaction()
      
             try {
      
              var created_user = await trx('users').insert({
                  name,
                  avatar,
                  whatsapp,
                  bio
              })
      
              var user_id = created_user[0]
             
             
      
              const insertedclassesID = await trx('classes').insert({
                  subject,
                  cost,
                  user_id
              })
  </code></pre>
  <p>I've got do this with SQLite3, but not with Postgres.
  I have to change the ORM Knex to Sequelize for Exemple?</p>
  <p>I know is a lot of questions But i'm needing help!</p>
  <p>THANKS FOR HELP ME!</p>