Strapi: Defining relationship

Created on 17 Dec 2017  路  3Comments  路  Source: strapi/strapi

Node.js version: 9.3.0
npm version: 5.6.0
Strapi version: 3.0.0-alpha.7.2
Operating system: macOS High Sierra 10.13.2

I'm completely new to Strapi and is trying Strapi 3, seems like a great system, but having a bit of issue with defining relationship.

I'm using both the admin and code (follow the docs) to define relationship between 2 models but getting issues with both way, both result in error with strapi and make the server stop running.

Basically I'm having 2 models, Picture and PictureTypes, and want to define many to many relationship between the two.
If I define the relationship through admin, adding a types field like attached pic, it'll add something like this to model settings:

"types": {
      "collection": "pictureTypes",
      "via": "pictures",
      "dominant": true
    }

And so, it results in error in console:

/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/mongodb/lib/mongo_client.js:433
          throw err
          ^

TypeError: Cannot read property 'attributes' of undefined
    at Object.getNature (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/strapi-utils/lib/models.js:86:63)
    at _.forEach (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/strapi-mongoose/lib/index.js:219:51)
    at /Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/lodash/lodash.js:4944:15
    at baseForOwn (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/lodash/lodash.js:3001:24)
    at /Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/lodash/lodash.js:4913:18
    at Function.forEach (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/lodash/lodash.js:9359:14)
    at _.forEach (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/strapi-mongoose/lib/index.js:218:17)
    at /Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/lodash/lodash.js:4944:15
    at baseForOwn (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/lodash/lodash.js:3001:24)
    at /Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/lodash/lodash.js:4913:18
    at Function.forEach (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/lodash/lodash.js:9359:14)
    at mountModels (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/strapi-mongoose/lib/index.js:166:15)
    at NativeConnection.instance.connection.on (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/strapi-mongoose/lib/index.js:307:11)
    at NativeConnection.emit (events.js:159:13)
    at /Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/mongoose/lib/connection.js:860:13
    at connectCallback (/Applications/MAMP/htdocs/Node/api/dogcatbot/node_modules/mongodb/lib/mongo_client.js:515:5)

And then I add one more attribute to PictureTypes as like in the docs:

"pictures": {
      "collection": "picture",
      "via": "types"
    }

Then try to start the server again, and still get the same error.

Am I doing something wrong here? Or it's a bug? Thanks.

medium bug

Most helpful comment

Hi @Aurelsicoko
Thanks for quick response. I've tried changing it all to lowercase and it works fine now. Seems like that's the issue.

Look forward to merge.

All 3 comments

@peeomid Can you try to lowercase your collection name?

"types": {
   "collection": "picturetypes",
   "via": "pictures",
   "dominant": true
}
"pictures": {
  "collection": "picture",
  "via": "types"
}

Btw, we are going to merge a new branch that will drastically improve the errors returned by the framework to know exactly what attribute is wrong in your models.

Hi @Aurelsicoko
Thanks for quick response. I've tried changing it all to lowercase and it works fine now. Seems like that's the issue.

Look forward to merge.

Amazing!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

isabellachen picture isabellachen  路  3Comments

quibaritaenperdresatrompe picture quibaritaenperdresatrompe  路  3Comments

OrestTs picture OrestTs  路  3Comments

guillermodoghel picture guillermodoghel  路  3Comments

profjefferson picture profjefferson  路  3Comments