title: Seeding table in KnexJS foreign key constraint
body: <p>I am building a NodeJS API backend and using Knex to handle DB migrations et al.</p>
  
  <p>I have 3 tables: <code>stalls</code>, <code>markets</code> and <code>bookings</code></p>
  
  <p>In my <code>bookings</code> table I want to reference the <code>id</code> column from both the <code>stalls</code> and <code>markets</code> tables.</p>
  
  <p>I have setup my <code>bookings</code> migration as follows:-</p>
  
  <pre><code>    exports.up = function(knex, Promise) {
    return knex.schema.createTable("bookings", table =&gt; {
      table.increments("id");
      table.string("firstName");
      table.string("lastName");
      table.string("address");
      table.string("phoneNumber");
      table.string("emailAddress");
      table
        .integer("marketDateId")
        .references("id")
        .inTable("markets");
      table
        .integer("stallTypeId")
        .references("id")
        .inTable("stalls");
      table.boolean("clothesRail");
      table.string("businessName");
      table.boolean("businessInsurance");
      table.string("items");
    });
  };
  
  exports.down = function(knex, Promise) {
    return knex.schema.dropTable("bookings");
  };
  </code></pre>
  
  <p>Stalls:</p>
  
  <pre><code>    exports.up = (knex, Promise) =&gt; {
    return knex.schema.createTable("stalls", table =&gt; {
      table.increments("id");
      table.string("stallType");
    });
  };
  
  exports.down = (knex, Promise) =&gt; {
    return knex.schema.dropTable("stalls");
  };
  </code></pre>
  
  <p>Markets:</p>
  
  <pre><code>exports.up = (knex, Promise) =&gt; {
    return knex.schema.createTable("markets", table =&gt; {
      table.increments("id");
      table.string("marketDate");
    });
  };
  
  exports.down = (knex, Promise) =&gt; {
    return knex.schema.dropTable("markets");
  };
  </code></pre>
  
  <p>I have seeded the <code>stalls</code> and <code>markets</code> tables with some dummy data, but when I try to seed the <code>bookings</code> table using the below seed file:</p>
  
  <p><code>bookings.js</code></p>
  
  <pre><code>exports.seed = (knex, Promise) =&gt;
    knex("bookings")
      .del()
      .then(() =&gt;
        knex("bookings").insert([
          {
            firstName: "Test",
            lastName: "Tester",
            phoneNumber: "07123 123456",
            emailAddress: "test@lb-dev.io",
            marketDateId: "7",
            stallTypeId: "7",
            clothesRail: false,
            items: "Testing Stuff"
          },
          {
            firstName: "Test",
            lastName: "Testington",
            phoneNumber: "07123 123456",
            emailAddress: "test@lb-dev.io",
            marketDateId: "8",
            stallTypeId: "8",
            clothesRail: false,
            businessName: "TestCrafts",
            items: "Testing Stuff"
          },
          {
            firstName: "Test",
            lastName: "Tester",
            phoneNumber: "07123 123456",
            emailAddress: "test@lb-dev.io",
            marketDateId: "9",
            stallTypeId: "9",
            clothesRail: true,
            businessName: "TestBusiness",
            businessInsurance: true,
            items: "Testing Stuff"
          }
        ])
      );
  </code></pre>
  
  <p>I get the following error on running <code>knex seed:run</code></p>
  
  <p><code>update or delete on table "markets" violates foreign key constraint "bookings_marketdateid_foreign" on table "bookings"</code></p>