title: How to save one-to-many relationship after insert in a transaction
body: <p>I am using Bookshelf v0.10.3 with Knex v0.12.9, in an Express v4.15.2 server on Node v6.9.5.</p>
  
  <p>I have a <code>user</code> table with an associated <code>identity</code> table, joined by an <code>identity_user</code> table. The tables are created using Knex:</p>
  
  <pre><code>Promise.all([
    knex.schema.createTable('user', table =&gt; {
      table.increments('id');
      table.string('username');
      table.string('password');
      table.unique('username');
    }),
    knex.schema.createTable('identity', table =&gt; {
      table.increments('id');
      table.string('type');
      table.string('value');
    }),
    knex.schema.createTable('identity_user', table =&gt; {
      table.integer('user_id').references('user.id');
      table.integer('identity_id').references('identity.id');
      table.unique(['user_id', 'identity_id']);
    })
  ]);
  </code></pre>
  
  <p>My Bookshelf models look like this:</p>
  
  <pre><code>bookshelf.plugin([ 'registry', 'bookshelf-camelcase' ]);
  
  const Identity = bookshelf.model('Identity', {
    tableName: 'identity',
  
    user() {
      return this.belongsTo('User', 'identity_user');
    }
  });
  
  const User = bookshelf.model('User', {
    tableName: 'user',
  
    initialize() {
      this.on('saving', model =&gt; {
        if (model.hasChanged('username')) {
          return model.set('username', String(model.attributes.username).toLowerCase());
        }
      });
      this.on('saving', model =&gt; {
        if (model.hasChanged('password')) {
          return bcrypt.hash(model.attributes.password, 10)
            .then(hash =&gt; model.set('password', hash));
        }
      });
    },
  
    identities() {
      return this.hasMany('Identity', 'identity_user');
    }
  });
  </code></pre>
  
  <p>In my <code>createUser</code> function, I'm trying to insert a new user and an identity in a single transaction.</p>
  
  <pre><code>function createUser(req, res, next) {
    const { email, username, password } = req.body;
  
    // some data validation
  
    return bookshelf.transaction((t) =&gt; {
      return new User({ username, password } ).save(null, { transacting: t })
        .then(user =&gt; new Identity({ type: 'email', value: email }).save({ userId: user.attributes.id }, { transacting: t }))
        .then(() =&gt; res.sendStatus(204))
        .catch(err =&gt; {
          // handle PostgreSQL unique violation error
        });
    });
  }
  </code></pre>
  
  <p>When I run the server and attempt to register a new user, I get the following error:</p>
  
  <pre><code>insert into "identity" ("type", "user_id", "value") values ($1, $2, $3) returning "id" - column "user_id" of relation "identity" does not exist { error: insert into "identity" ("type", "user_id", "value") values ($1, $2, $3) returning "id" - column "user_id" of relation "identity" does not exist
  </code></pre>
  
  <p>This is a PostgreSQL error (code 42703 - Undefined Column), but everything seems to be set up correctly. I could really use another set of eyes on this. What am I missing?</p>
  
  <p>Thanks in advance!</p>