I’m creating a user database and when I call the method to delete a user I get this error:
Column not found: 1054 Unknown column ‘user_id’ in ‘where clause’
I am deleting from my user table using the Bake created code below:
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
However the query that creates the error is:
DELETE FROM users_paths WHERE user_id = :c0
I have no idea where it is getting the users_path table. I do have that table in my database, but it should not be called in this context. Where could this table be coming from, and how do I change it?
As your users belongsToMany Paths when you delete the user Cake deletes the associated Paths also.
The error message says there is no user_id filed in users_paths database table.
Aside: By using cake conventions you can save a lot of time for yourself.
Just to be clear, CakePHP won’t delete the Paths, but will delete the join table entries (i.e. user_paths). What does your user_paths schema look like?