Skip to main content

Relationships

A Relationship represent a connection between two collections / models.

There is basically multiple type of relationships between models such as:

  • hasMany
  • belongsTo or hasOne

for Sequelize collections, and the ref property for Mongoose models.

Adminmate is working well without configured relationships, but we highly recommend to use them as they add some interesting features in your back-office.

If you used the Adminmate CLI to install the Data API, the relationships are automatically added to your collections / models. If it's not the case you can check the different codes bellow depending your database.

MySQL, PostgreSQL, SQLite​

Bellow an example of a hasMany relationship between User & Car collection.

module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('users', {
...
});

User.associate = (models) => {
User.hasMany(models.cars);
};

return User;
};

MongoDB​

Bellow an example of a ref relationship between User & Car collection.

module.exports = (mongoose, Mongoose) => {
const schema = Mongoose.Schema({
...
user_id: { type: Mongoose.Schema.Types.ObjectId, ref: 'users' },
...
});

return mongoose.model('cars', schema, 'cars');
};