๐Ÿ“ฆ fghpdf / pasture

๐Ÿ“„ one-to-many.md ยท 34 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34One-to-many associations can be created with {@link Model#belongsTo belongsTo}, {@link Model#hasMany hasMany}, {@link Model#morphMany morphMany} / {@link Model#morphTo morphTo}, and some of the {@link Model#through through} relation types.

    var Book = bookshelf.Model.extend({
      tableName: 'books',
      pages: function() {
        return this.hasMany(Page);
      }
    });

    var Page = bookshelf.Model.extend({
      tableName: 'pages',
      book: function() {
        return this.belongsTo(Book);
      }
    });

A Knex migration for the above relationship could be created with:

    exports.up = function(knex, Promise) {
      return knex.schema.createTable('books', function(table) {
        table.increments('id').primary();
        table.string('name');
      }).createTable('pages', function(table) {
        table.increments('id').primary();
        table.string('content');
        table.integer('book_id').references('books.id')
      });
    };

    exports.down = function(knex, Promise) {
      return knex.schema.dropTable('books')
        .dropTable('pages');
    };