Informations
What is the current behavior?
After creating relation between content-types, strapi doesn't restart. Console says:
[2018-05-07T14:46:37.554Z] error Something went wrong in the model Container with the attribute contents
[2018-05-07T14:46:37.557Z] error Cannot read property 'models' of undefined
Steps to reproduce the problem
Do everything in the strapi UI and always choose the defaults that strapi suggests.
Create two content types "Container" and "Content". They both have a field "Title". Add a relation to "Container": One "Container" belongs to many "Content". Then save. Strapi wants to restart. Restart will not happen. Console says:
[2018-05-07T14:46:37.554Z] error Something went wrong in the model Container with the attribute contents
[2018-05-07T14:46:37.557Z] error Cannot read property 'models' of undefined
What is the expected behavior?
Strapi should restart and approve my content-types with their relation.
Suggested solutions
I also did the following, which worked:
I created content-type "Container", then added the relation to "Container": One "Container" belongs to many "User".
I tried it because in the Content-Type-Manager when creating a relation to "User" things are displayed differently than with my own content-types. When i choose "User", then the title of the selector dropdown will also show "User (User-permissions)" but when i choose "Content" the title of the selector dropdown will just be blank. I'm not sure if it has something to do with that bug.
I tried again: Again everything was the same as described above. But then i tried to do the relation "the other way around": I added the relation to "Content": One "Container" has many "Content". It worked. And also the title of the selector dropdown will never be blank and always shows the correct content-type.
I have currently no suggestion how to solve this.
Same problem...
@dsheyp Can you try to downgrade to Node v9 and test, I don't believe any testing has been done on Node v10 and it could be whats causing this.
Hello @dsheyp I tried and it works on my side. Can you try with the last version of Strapi.
Can you share with me your 2 models please.
I updated Strapi to the latest version but as I got things working for me with the "relation the other way round" I don't want to downgrade Node - this is too much change @ the moment - I have to go on with my Strapi-based project, someone is waiting for it ;-) So I kept my current project and added the two content-types as described above. The problem persists. Strapi can't restart. I have to remove the content-types manually from the filesystem and from mongodb. Only then Strapi can restart. Still exactly the same message in the console as described.
Hi @lauriejim I put the models on https://github.com/dsheyp/strapi-models as they were created by the Content-Type-Manager
I added a Screenshot showing the dropdown title being blank.
Added: I didn't have to remove the models from mongodb. Nothing was created in mongodb by the content-type-manager. Just in the filesystem.
Exactly the same problem, the relations is created in one of ....settings.json model files but not on the second file, so I need to add this manually in the other file...
I have Node 9.11.1 and i'm experiencing the same issue so i don't think downgrading would change anything
This seems to appear at random, as I've done tests that it works fine, and others that will duplicate this exact same error. It seems to break when writing to one side of the relation.
What I discribed earlier happened again. This time I tried to manually fix the models. I had to insert a relation. It still didn't work. There was one field plugin: " " When I removed that empty field it worked. Could the reason for this bug be found in that empty plugin: " " field?
Seeing that there is problem with the plugin: " " attribute, I just want to mention that this may be related to #886. The fix is going to be released in alpha.12.2
It seems to be a duplicate of https://github.com/strapi/strapi/issues/886. It has been fixed (https://github.com/strapi/strapi/pull/1081) but not released yet.
Thanks @Aurelsicoko, So I'm waiting for 12.2 release. :wink:
@Aurelsicoko I'm having this issue in 12.2 when creating a relation.
@Aurelsicoko I also confirm that this issue still exists in 12.2. :thinking:
I add some relation and my server stopped after save and not working at all.
Please reopen this issue for more info.
/CC: @soupette, @lauriejim
@mnlbox & @indiecore-fm Did you try with a fresh new application ?
@lauriejim Yes, I create new app and copy my api folder in new app and then login to dashboard and create my relations.
I follow this migration guide but replace 3.0.0-alpha.11.1 by 3.0.0-alpha.12.2 https://strapi.io/documentation/migration/migration-guide-alpha-10-to-alpha-11.html
@mnlbox there is a breaking change on services code of your generated API. Please check https://github.com/strapi/strapi/wiki/migration-guide-alpha.12.1-to-alpha.12.2 for APIs migration
I tested this out as well with a fresh installation. Unfortunately, it seems that a new bug has appeared. It has a different error message from what I have experienced before.
[2018-05-25T01:51:46.804Z] error Something went wrong in the model `Testspecification` with the attribute `test`
[2018-05-25T01:51:46.807Z] error Cannot read property 'hasOwnProperty' of undefined
I'm guessing this has to do with not checking for whether the object actually exists or not, judging from Object.hasOwnProperty.
@Xcross I think it's a old issue because I see it 4 month later until now. I think Strapi need more robust code review specially about relation part of framework.
It's related issues for more info:
@mnlbox the problem is, the strapi team also need to be provided with the exact ways in which to reproduce the error to help track down the cause.
@mnlbox I opened #886. The error I experienced and the issues you provided all point to this error message Cannot read property 'models' of undefined, which I believe is fixed in this current state by removing the plugin attribute. Now this different error message appeared without plugin so I think this is a new bug, unless the code behind all these is fundamentally flawed.
Can everyone confirm me that you have an empty plugin attribute in your models? It was the cause of the previous issue, it should be fixed but it seems that a new case hasn't been covered.
@XcrossD Same issue. I posted it here:
the one who fixes this strapi-not-booting-issue in the near future will be allowed to carry the title "bootstrapi" thenceforth as a sign of honour 馃憤
hi
i just tested with alpha 12.2 with a fresh project resulting in the server not being able to restart after adding a relation between two content-types. also the title of the selector dropdown was still blank as it was in my initial issue description. the console shows
[2018-05-25T19:05:43.804Z] error Something went wrong in the model `Test` with the attribute `subtests`
[2018-05-25T19:05:43.807Z] error The attribute `test` is missing in the model Subtest
the empty plugin: "" attribute is gone
after adding the relation manually to one of the content-types the server restarts and the relation works as expected
What is the browser you use ?
@Aurelsicoko No I don't see plugin attribute. This error is worse than before because previously if I adjust settings.json files manually I can get it started and also have the relations created. Now the only way to get started is to remove all the extra settings created when adding a relation.
@indiecore-fm I agree the issue should be the same if it's along the lines of error Cannot read property 'xxxxxxx' of undefined. However I am confused by what @dsheyp observed. The initial observation shows the same error, but later on it seems like strapi just didn't generate the json files correctly judging by the updated error message error The attribute 'test' is missing in the model Subtest. He also managed to start up, but for me I can't event start even correcting my json files according to the relations guideline.
Are the steps to reproduce the exact same?
@Aurelsicoko Yes.
Having the same issue here - New install, created 2 new content types, added a couple of string fields to the first, then created a relation to the second, crashed and won't start again :(
same error in 3.0.0-alpha.12.6 :-(
@nicholas-c @lauborges Can you give us more details to reproduce it? Is this the exact same again?
@Aurelsicoko I have noticed something similar when using camelCase in a relation name on some of them. Strapi is creating the relation model using the same camelCase instead of dropping it to lower case.
IE:
"model" : "testModel"
instead of
"model": "testmodel"
hi @Aurelsicoko
After several tests, I saw that I cannot create a relationship with an entity using camelCase.
in my case:
establishment can have an attribute called establishmenttype but can't if the attribute name is establishmentType
$ debug POST /content-type-builder/models/ (76 ms)
$ error Something went wrong in the model `Establishment` with the attribute `establishmentType`
$ error Cannot read property 'attributes' of undefined
I've experienced the same issue when I was developing a new project... It was totally random. The attribute was correctly added in the edited model but it wasn't the case for the other model.
However, I have a little theory. Can you confirm me that you're working with an external database? I think the slow of the request can be the cause of an unexcepted behaviour of the Content-Type Builder.
@Aurelsicoko when I experienced this error myself I was too using an external DB.
@Aurelsicoko I too have encountered this while using an external DB. Hope it helps!
@Aurelsicoko I think it's not related to database type because I experienced this error with local MongoDB instance.
I am having the same issue. Using a local MongoDB instance.
I updated packagaes/strapi-hook-bookshelf/lib/relations.js as described in a previous issue (https://github.com/strapi/strapi/pull/892)
That issue was updating packages/strapi-plugin-content-manager/config/queries/mongoose.js, but it looks like the functionality has been factored out to this current hook.
I replaced the 'oneToOne' block as shown below.
`switch (association.nature) {
...
case 'oneToOne':
if (response[current] !== params.values[current]) {
const value = _.isNull(params.values[current]) ? response[current] : params.values;
const recordId = _.isNull(params.values[current]) ? value[this.primaryKey] || value.id || value._id : value[current];
if (response[current] && _.isObject(response[current]) && response[current][this.primaryKey] !== value[current]) {
virtualFields.push(
strapi.query(details.collection || details.model, details.plugin).update({
id: response[current][this.primaryKey],
values: {
[details.via]: null
},
parseRelationships: false
})
);
}
// Remove previous relationship asynchronously if it exists.
virtualFields.push(
strapi.query(details.model || details.collection, details.plugin).findOne({ id : recordId })
.then(record => {
if (record && _.isObject(record[details.via])) {
virtualFields.push(
module.exports.update.call(this, {
id: record[details.via][this.primaryKey] || record[details.via].id,
values: {
[current]: null
},
parseRelationships: false
})
);
}
return Promise.resolve();
})
.then(response => {
// Updating the new relations
// When params.values[current] is null this means that we are removing the relation.
// Recreate relation on the first side
virtualFields.push(
strapi.query(details.model || details.collection, details.plugin).update({
id: recordId,
values: {
[details.via]: _.isNull(params.values[current]) ? null : value[this.primaryKey] || value.id || value._id
},
parseRelationships: false
})
);
// Recreate relation on the other side if the value is not null
if (!_.isNull(params.values[current])) {
virtualFields.push(
module.exports.update.call(this, {
id: value[this.primaryKey] || value.id || value._id,
values: {
[current]: recordId
},
parseRelationships: false
})
);
}
})
);
acc[current] = _.isNull(params.values[current]) ? null : value[current];
}
break;
/*
case 'oneToOne':
if (response[current] !== params.values[current]) {
const value = _.isNull(params.values[current]) ? response[current] : params.values;
const recordId = _.isNull(params.values[current]) ? getValuePrimaryKey(value, this.primaryKey) : value[current];
const model = module.exports.getModel(details.collection || details.model, details.plugin);
// Remove relation in the user side.
virtualFields.push(
module.exports.findOne
.call(model, { [model.primaryKey]: recordId }, [details.via])
.then(record => {
if (record && _.isObject(record[details.via])) {
return module.exports.update.call(this, {
id: getValuePrimaryKey(record[details.via], model.primaryKey),
values: {
[current]: null
},
parseRelationships: false
});
}
return Promise.resolve();
})
.then(() => {
return module.exports.update.call(model, {
id: getValuePrimaryKey(response[current] || {}, this.primaryKey) || value[current],
values: {
[details.via]: null
},
parseRelationships: false
});
})
.then(() => {
if (!_.isNull(params.values[current])) {
return module.exports.update.call(model, {
id: recordId,
values: {
[details.via]: getValuePrimaryKey(params, this.primaryKey) || null
},
parseRelationships: false
});
}
return Promise.resolve();
})
);
acc[current] = _.isNull(params.values[current]) ? null : value[current];
}
break;
*/
`
The relationship is only maintained on one side of the relation.
@mnlbox Bad news... my theory doesn't work anymore. How many models do you have? I still think it's related to the execution time of something (request, number of models)
@Aurelsicoko I just have 3 models. I don't know how I can help you to catch this guys :disappointed:
Looks having been fixed, but the PR not link this issue because it looks good on my side. Can you give us an update with last version of Strapi.
@lauriejim I stated this in another issue when building the relation shouldn't strapi just force lowercase when referencing another model? model.tolowercase() ?
From my testing this appears to be browser related on model relation creation in the dropdown of the admin ui, latest chrome works vs latest firefox fails for me.
@krisballistiq This makes me crazy... but your comment helps me a lot to have a better overview of this issue. When we fixed the issue, I also discovered it was browser related. The issue came back for no reason, but it would make sense if it works again on Chrome but failed using Firefox.
It could mean that we are using an unstable prototype JS function in this plugin. Or even worst, we might use loop methods like map, filter or reduce where the execution order is not guaranteed which could cause the issue depending on how JS is interpreted by the browser...
Move the conversation on #2097
Most helpful comment
hi @Aurelsicoko
After several tests, I saw that I cannot create a relationship with an entity using camelCase.
in my case:
establishment can have an attribute called establishmenttype but can't if the attribute name is establishmentType