# Delete Function is throwing SQL Error

**URL:** <https://discourse.cakephp.org/t/delete-function-is-throwing-sql-error/12209>\
**Category:** Need Help\
**Created:** [August 28, 2024, 9:32am UTC](https://discourse.cakephp.org/t/delete-function-is-throwing-sql-error/12209 "2024-08-28T09:32:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Howan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/howan/32/3182_2.png) [@Howan](https://discourse.cakephp.org/u/Howan)\
**Post date:** [August 28, 2024, 9:32am UTC](https://discourse.cakephp.org/t/delete-function-is-throwing-sql-error/12209/1 "2024-08-28T09:32:08Z")

</div>

Hey it’s me again. ☺

I try to delete a user and get the following error.

```auto
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`cake_cms`.`articles`, CONSTRAINT `articles_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))

```

I assume i can’t delete a USER without deleting also all depending ARTICLES also.  
I think this dependency is written in the UsersTable.php but i dont know how i have to change it  
to not get this error.

Thank you so much.

```auto
// UsersController.php
public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $user = $this->Users->get($id);

        $this->Authorization->authorize($user);

        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']);
    }

```

```auto
// UsersTable.php
class UsersTable extends Table
{
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('users');
        $this->setDisplayField('email');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('Articles', [
            'foreignKey' => 'user_id',
        ]);
    }

```

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [August 28, 2024, 9:38am UTC](https://discourse.cakephp.org/t/delete-function-is-throwing-sql-error/12209/2 "2024-08-28T09:38:29Z")

</div>

since users and articles belong to each other (and are connected via a foreign key constraint in your table) you need to tell CakePHP, that they are “dependent”

See [Deleting Data - 5.x](https://book.cakephp.org/5/en/orm/deleting-data.html#cascading-deletes)

So you need

```auto
$this->hasMany('Articles', [
    'foreignKey' => 'user_id',
    'dependent' => true,
    'cascadeCallbacks' => true,
]);

```

so that all articles associated to a user are also deleted.
