{"action":"opened","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1077","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1077/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1077/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1077/events","html_url":"https://github.com/tgriesser/bookshelf/issues/1077","id":124550718,"number":1077,"title":"automatic generation of models","user":{"login":"eugen35","id":8413281,"avatar_url":"https://avatars.githubusercontent.com/u/8413281?v=3","gravatar_id":"","url":"https://api.github.com/users/eugen35","html_url":"https://github.com/eugen35","followers_url":"https://api.github.com/users/eugen35/followers","following_url":"https://api.github.com/users/eugen35/following{/other_user}","gists_url":"https://api.github.com/users/eugen35/gists{/gist_id}","starred_url":"https://api.github.com/users/eugen35/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eugen35/subscriptions","organizations_url":"https://api.github.com/users/eugen35/orgs","repos_url":"https://api.github.com/users/eugen35/repos","events_url":"https://api.github.com/users/eugen35/events{/privacy}","received_events_url":"https://api.github.com/users/eugen35/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-01-01T16:57:27Z","updated_at":"2016-01-01T16:57:27Z","closed_at":null,"body":"hello, tim! Happy New Year! Success and achievements!\r\n\r\n## i try to automatically create models, but something wrong(\r\n\r\ni do like this:\r\n\r\n     var _cModels=require('./crm-cfg').models, // JSON config with model description\r\n           _ormModels={}; // there will be generated model classes.\r\n\r\n     //Create Models without relations\r\n    for (var modelClassName in _cModels) {  \r\n     _ormModels[modelClassName]=bookshelf.Model.extend({tableName:_.snakeCase(_cModels[model     ClassName].plural), myNameIs:modelClassName});\r\n     }\r\n\r\n     //Create relations in Models\r\n    for (var modelClassName in _ormModels) { //Создаём взаимосвязи между моделями\r\n        createRelationsInORMModel(_cModels[modelClassName], modelClassName);\r\n     }\r\n     \r\n      //Create relations in One Model\r\n      function createRelationsInORMModel(cModel, modelClassName) {\r\n         var modelProto=cModel.objProto, relatedORMModel;\r\n          for (var key in modelProto) {\r\n          \r\n             switch (true) {\r\n                      case ('belongsTo' in modelProto[key]):\r\n                    relatedORMModel=_ormModels[modelProto[key].belongsTo];\r\n                    _ormModels[modelClassName].prototype[relatedORMModel]=function(){\r\n                        return this.belongsTo(relatedORMModel);\r\n                    };\r\n                    break;\r\n                case ('hasOne' in modelProto[key]):\r\n                    relatedORMModel=_ormModels[modelProto[key].hasOne];\r\n                    _ormModels[modelClassName].prototype[relatedORMModel]=function(){\r\n                       return this.hasOne(relatedORMModel);\r\n                     };\r\n                    break;\r\n                case ('hasMany' in modelProto[key]):\r\n                    relatedORMModel=_ormModels[modelProto[key].hasMany];\r\n                    _ormModels[modelClassName].prototype[relatedORMModel]=function(){\r\n                        return this.hasMany(relatedORMModel);\r\n                    };\r\n                    break;\r\n                case ('belongsToMany' in modelProto[key]):\r\n                    relatedORMModel=_ormModels[modelProto[key].belongsToMany];\r\n                    _ormModels[modelClassName].prototype[relatedORMModel]=function(){\r\n                        return this.belongsToMany(relatedORMModel);\r\n                    };\r\n                    break;\r\n            }\r\n        }\r\n    }\r\n\r\n-----------------------------------------------------------------------------\r\nModels are created, but they work particularly with related Models (   !?\r\n-----------------------------------------------------------------------\r\nIf the model has only relation with one model (one to many or one to one etc), then it usually work to fetch withRelated model.\r\n\r\n## If the model has relations with multiple different models, so this is  the my bug !!!!!!!!!: \r\n\r\nwhen fetch withRelated:['Model1', 'Model2', 'Model3'] all related models are equal to only one of them. So if i call MyModel.related('Model2').get('name') or MyModel.related('Model1').get('name'), i will get Model3.name. !? \r\nOpposite, when i call MyModel.related('Model3').get('name') i will get  Model3.name. \r\n"}}