Relationships

What is a relationship?

A relationship is a connection between two collections.

Relationships are visible and actionnable in Forest Admin:

  • hasMany (1)

  • belongsTo or hasOne(2)

Note that Lumber only automatically generates belongsTo relationships, based on your foreign keys. hasMany or hasOne relationships will have to be added manually.

Some ORMs do not create foreign key constraints. This means that in some cases, you will also have to add belongsTo relationships manually.

Adding relationships

Depending on your database type, Lumber will have generated models in Sequelize (for SQL databases) or Mongoose (for Mongo databases).

Below are some simple snippets showing you how to add relationships. However, should you want to dig deeper, please refer to the appropriate framework's documentations:

Adding a hasMany relationship

In our Live demo, a customer can have multiple orders. In that case, we have to use a hasMany relationship.

SQL
Mongodb
/models/customers.js
module.exports = (sequelize, DataTypes) => {
var Customer = sequelize.define('customers', {
// ...
}, {
// ...
});
Customer.associate = (models) => {
Customer.hasMany(models.orders);
};
return Customer;
};
/models/customers.js
const mongoose = require('mongoose');
const schema = mongoose.Schema({
...
'orders': [{ type: mongoose.Schema.Types.ObjectId, ref: 'orders' }],
...
}, {
timestamps: true,
});
module.exports = mongoose.model('customers', schema, 'customers');

Note that for orders to be displayed within the related data section of your customer, they have to be populated in your database. For instance:

Once you've added your relationship(s) in your model(s), they will only be taken into account after you restart your server.

Adding a hasOne relationship

hasOne relationships are the opposite of belongsTo relationships. Taking the same example as before, the opposite of the Address belongsTo a Customer is simply a customerhasOne address.

SQL
Mongodb
/models/customers.js
module.exports = (sequelize, DataTypes) => {
var Customer = sequelize.define('customers', {
// ...
}, {
// ...
});
Customer.associate = (models) => {
Customer.hasOne(models.addresses);
};
return Customer;
};
/models/customers.js
const mongoose = require('mongoose');
const schema = mongoose.Schema({
...
'address': { type: mongoose.Schema.Types.ObjectId, ref: 'addresses' },
...
}, {
timestamps: true,
});
module.exports = mongoose.model('customers', schema, 'customers');

Don't forget to restart your server for your newly added relationships to be taken into account.

Adding a belongsTo relationship

On our Live Demo example, the Address model has a foreignKey customer_id that points to the Customer. In other words, an addressbelongsTo a customer.

SQL
Mongodb
/models/addresses.js
module.exports = (sequelize, DataTypes) => {
let Address = sequelize.define('addresses', {
// ...
}, {
// ...
});
Address.associate = (models) => {
Address.belongsTo(models.customers);
};
return Address;
};
/models/addresses.js
const mongoose = require('mongoose');
const schema = mongoose.Schema({
...
'customer_id': { type: mongoose.Schema.Types.ObjectId, ref: 'customers' },
...
}, {
timestamps: true,
});
module.exports = mongoose.model('addresses', schema, 'addresses');

Don't forget to restart your server for your newly added relationships to be taken into account.