-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.js
28 lines (20 loc) · 894 Bytes
/
models.js
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
const Sequelize = require('sequelize')
const db = new Sequelize('postgres://localhost:5432/plantr')
module.exports = db
//When you create a belongsToMany relationship, you must specify an extra second parameter with an options object. In this object, you use "through" to specify the name of the join table that will contain their foreign keys, and Sequelize will automatically create it for you!
const Gardener = db.define('gardner', {
name: Sequelize.STRING,
age: Sequelize.INTEGER
})
const Plot = db.define('plot', {
size: Sequelize.INTEGER,
shaded: Sequelize.STRING
})
const Vegetable = db.define('vegetable', {
name: Sequelize.STRING,
color: Sequelize.STRING,
planted_on: Sequelize.DATE
})
Vegetable.belongsToMany(Plot, {through: 'vegetable_plot'})
Plot.belongsToMany(Vegetable, {through: 'vegetable_plot'})
Gardener.belongsTo(Vegetable, {as: 'favorite_vegetable'})