Search code examples
sails.jssails-mongo

can we create two model or controller for same collection in sails js


I am new to sails js. I am using mongodb database. I have users collection. I have created api using sails generate api users it creates Users.js and UsersController.js files. I want to know that can we create another controller or model for the same collection?

I have created User_dummy.js and User_dummyController.js using same command given above. And copied the content of the file Users.js into User_dummy.js and UsersController.js file into User_dummyController.js. But it doesn't work.

Its giving me following error :

Trying to associate a collection attribute to a model that doesn't have a Foreign Key.

I want to make two copies of both model and controller for users collection. Is there any solution or is there any way to create two controller or model.


Solution

  • You should be able to use the tableName model setting (see documentation) to have a shared collection for two or more models.

    Users.js/User_dummy.js:

    module.exports = {
    
      tableName: 'users',
    
      attributes: {
    
      }
    };