Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot delete data by ID if model has a relation on MySQL #1419

Closed
derrickmehaffy opened this issue Jun 19, 2018 · 9 comments
Closed

Cannot delete data by ID if model has a relation on MySQL #1419

derrickmehaffy opened this issue Jun 19, 2018 · 9 comments
Assignees
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members

Comments

@derrickmehaffy
Copy link
Member

Informations

  • Node.js version: v10.4.1
  • npm version: 6.1.0
  • Strapi version: 3.0.0-alpha.12.5
  • Database: 10.2.15-MariaDB
  • Operating system: Linux Mint 19 Beta (Ubuntu 18.04)

What is the current behavior?

If a model has a relation (in my testing on a fresh clean project, a many to one) you cannot delete data, provided the following error:

TypeError: System.forge(...)[association.alias](...).detach is not a function
    at Promise.all.System.associations.map.association (/mnt/nixstorage/Github/capiv2-dev/api/system/services/System.js:138:51)
    at Array.map (<anonymous>)
    at Object.remove (/mnt/nixstorage/Github/capiv2-dev/api/system/services/System.js:137:27)
    at destroy (/mnt/nixstorage/Github/capiv2-dev/api/system/controllers/System.js:68:35)
    at dispatch (/usr/lib/node_modules/strapi/node_modules/koa-router/node_modules/koa-compose/index.js:44:32)
    at next (/usr/lib/node_modules/strapi/node_modules/koa-router/node_modules/koa-compose/index.js:45:18)
    at dispatch (/usr/lib/node_modules/strapi/node_modules/koa-compose/index.js:42:32)
    at policies.push (/usr/lib/node_modules/strapi/lib/middlewares/router/utils/routerChecker.js:59:26)
    at dispatch (/usr/lib/node_modules/strapi/node_modules/koa-compose/index.js:42:32)
    at module.exports (/mnt/nixstorage/Github/capiv2-dev/plugins/users-permissions/config/policies/permissions.js:24:20)

Steps to reproduce the problem

I created two models system and body which you can see below. Deleting works fine on both system and body if there is no relation between them. However if I add a relation between them, in my case one system can have many body I am provided with the error listed above.

System Model:

{
  "connection": "default",
  "collectionName": "systems",
  "info": {
    "name": "system",
    "description": ""
  },
  "options": {
    "increments": true,
    "timestamps": true,
    "comment": ""
  },
  "attributes": {
    "systemName": {
      "unique": true,
      "type": "string",
      "required": true
    },
    "edsmID": {
      "type": "integer"
    },
    "edsmID64": {
      "type": "float"
    },
    "edsmCoordX": {
      "type": "float"
    },
    "edsmCoordY": {
      "type": "float"
    },
    "edsmCoordZ": {
      "type": "float"
    },
    "edsmCoordLocked": {
      "type": "boolean"
    },
    "bodies": {
      "collection": "body",
      "via": "system"
    }
  }
}

Body Model:

{
  "connection": "default",
  "collectionName": "bodies",
  "info": {
    "name": "body",
    "description": ""
  },
  "options": {
    "increments": true,
    "timestamps": true,
    "comment": ""
  },
  "attributes": {
    "bodyName": {
      "type": "string",
      "required": true
    },
    "system": {
      "model": "system",
      "via": "bodies"
    }
  }
}

What is the expected behavior?

The delete function should work with or without a relation field

Suggested solutions

Modify the services file for MySQL to properly account for relations.

@lauriejim this is a breaking issue with MySQL, Relations, and Deletion.

@derrickmehaffy
Copy link
Member Author

Just confirmed as well that deleting the relation field allows you to delete entries by ID again.

@lauriejim lauriejim self-assigned this Jun 19, 2018
@lauriejim lauriejim added issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: have to reproduce labels Jun 19, 2018
@derrickmehaffy
Copy link
Member Author

Confirmed bug with oneWay Relations on the end model (source model is fine as it doesn't have a relation defined)

@derrickmehaffy
Copy link
Member Author

Also confirmed oneToOne issue on both sides.

@lauriejim lauriejim added status: can not reproduce Not enough information to reproduce and removed status: have to reproduce labels Jun 19, 2018
@lauriejim
Copy link
Contributor

I'm on master branch and I can't reproduce this issue.

Can you give me the data creation/deletion step you did please.

@derrickmehaffy
Copy link
Member Author

@lauriejim I'm just posting data via postman, delete in the same way

Showing running Strapi:
image

Model1:

image

Model2:

image

Posting data to a model called model1 with no relation data (model has relation setup but not sending data):

image

Attempting to delete that data via postman:

image

And a screenshot of the console:
image

@derrickmehaffy
Copy link
Member Author

Deleting via the AdminUI @lauriejim works fine its when you attempt to delete using the endpoint.

@lauriejim
Copy link
Contributor

lauriejim commented Jun 19, 2018

I was trying with the content manager. Let me test again with the API.

@derrickmehaffy
Copy link
Member Author

I think the issue is here:

/**
* Promise to remove a/an <%= id %>.
*
* @return {Promise}
*/
remove: async (params) => {
await Promise.all(
<%= globalID %>.associations.map(association =>
<%= globalID %>.forge(params)[association.alias]().detach()
)
);
return <%= globalID %>.forge(params).destroy();
}
};

At least comparing it to the Mongo Template here:

/**
* Promise to remove a/an <%= id %>.
*
* @return {Promise}
*/
remove: async params => {
// Select field to populate.
const populate = <%= globalID %>.associations
.filter(ast => ast.autoPopulate !== false)
.map(ast => ast.alias)
.join(' ');
// Note: To get the full response of Mongo, use the `remove()` method
// or add spent the parameter `{ passRawResult: true }` as second argument.
const data = await <%= globalID %>
.findOneAndRemove(params, {})
.populate(populate);
if (!data) {
return data;
}
await Promise.all(
<%= globalID %>.associations.map(async association => {
const search = _.endsWith(association.nature, 'One') || association.nature === 'oneToMany' ? { [association.via]: data._id } : { [association.via]: { $in: [data._id] } };
const update = _.endsWith(association.nature, 'One') || association.nature === 'oneToMany' ? { [association.via]: null } : { $pull: { [association.via]: data._id } };
// Retrieve model.
const model = association.plugin ?
strapi.plugins[association.plugin].models[association.model || association.collection] :
strapi.models[association.model || association.collection];
return model.update(search, update, { multi: true });
})
);
return data;
}
};

Though I'm not entirely sure how to fix this myself.

@derrickmehaffy
Copy link
Member Author

Thats if this template is what is being used to generate the files with the adminUI

@lauriejim lauriejim added status: confirmed Confirmed by a Strapi Team member or multiple community members and removed status: can not reproduce Not enough information to reproduce labels Jun 19, 2018
@lauriejim lauriejim added this to To Do🚦 in 3.0.0-alpha.12.7 via automation Jun 20, 2018
3.0.0-alpha.12.7 automation moved this from To Do🚦 to Done ✅ Jun 23, 2018
@lauriejim lauriejim added this to To Do🚦 in 3.0.0-alpha.12.6 via automation Jun 26, 2018
@lauriejim lauriejim removed this from Done ✅ in 3.0.0-alpha.12.7 Jun 26, 2018
@lauriejim lauriejim moved this from To Do🚦 to Done ✅ in 3.0.0-alpha.12.6 Jun 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members
Projects
No open projects
3.0.0-alpha.12.6
  
Done ✅
Development

No branches or pull requests

2 participants