Another nube question


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?

Thank you for any assistance,
Troy L. Marker


Have you tryed just getting id in the request


Or you sure it’s not user_id? Is the table a child table?
Go over this again


What is in your UsersTable.php?


rdd, my UsersTable.php is:

<?php namespace App\Model\Table; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; /** * Users Model * * @property \Cake\ORM\Association\BelongsTo $Users * @property \Cake\ORM\Association\BelongsToMany $Paths * * @method \App\Model\Entity\User get($primaryKey, $options = []) * @method \App\Model\Entity\User newEntity($data = null, array $options = []) * @method \App\Model\Entity\User[] newEntities(array $data, array $options = []) * @method \App\Model\Entity\User|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) * @method \App\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) * @method \App\Model\Entity\User[] patchEntities($entities, array $data, array $options = []) * @method \App\Model\Entity\User findOrCreate($search, callable $callback = null, $options = []) */ class UsersTable extends Table { /** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { parent::initialize($config); $this->setTable('users'); $this->setDisplayField('users_id'); $this->setPrimaryKey('users_id'); $this->belongsTo('AllUsers', [ 'className' => 'users', 'foreignKey' => 'users_id', 'joinType' => 'INNER' ]); $this->belongsToMany('Paths', [ 'foreignKey' => 'user_id', 'targetForeignKey' => 'path_id', 'joinTable' => 'users_paths' ]); } /** * Default validation rules. * * @param \Cake\Validation\Validator $validator Validator instance. * @return \Cake\Validation\Validator */ public function validationDefault(Validator $validator) { $validator ->allowEmpty('users_username'); $validator ->allowEmpty('users_password'); $validator ->allowEmpty('users_hint'); $validator ->allowEmpty('users_answer'); return $validator; } /** * Returns a rules checker object that will be used for validating * application integrity. * * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. * @return \Cake\ORM\RulesChecker */ public function buildRules(RulesChecker $rules) { $rules->add($rules->existsIn(['users_id'], 'Users')); return $rules; } }


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?


Yes, you are right. I was not clear in my answer.