orm icon indicating copy to clipboard operation
orm copied to clipboard

🐛 fkAction/fkOnDelete has no effect for automatically created migrations

Open wapmorgan opened this issue 2 years ago • 1 comments

No duplicates 🥲.

  • [X] I have searched for a similar issue in our bug tracker and didn't find any solutions.

What happened?

Problem: When fkAction or fkOnDelete is specified (has value that differs from CASCADE) for relation, it has not effect when creating a migration via cycle:migrate.

Example:

  • Entity has relation:
#[BelongsTo(target: Voucher::class, nullable: true, fkAction: 'SET NULL')]
    public ?Voucher $voucher = null;
  • and current schema:
voucher_id      integer
        constraint participants_foreign_voucher_id_61289758e3bf4
            references vouchers
            on update cascade on delete set null,
  • when cycle:migrate is called, I except change on update action to set null. But It creates migration, that changes on delete action to cascade in up method - it's incorrect!
$this->table('TABLE_NAME')
            ->alterForeignKey(["voucher_id"], 'vouchers', ["id"], [
                'name'   => 'participants_foreign_voucher_id_61289758e3bf4',
                'delete' => 'CASCADE',
                'update' => 'CASCADE'
            ])
            ->update();

In down method it correctly returns back all things (to current schema):

$this->table('TABLE_NAME')
            ->alterForeignKey(["voucher_id"], 'vouchers', ["id"], [
                'name'   => 'participants_foreign_voucher_id_61289758e3bf4',
                'delete' => 'SET NULL',
                'update' => 'CASCADE'
            ])
            ->update();

Version

php:8.1.10
postgresql 13
cycle/annotated                    v3.4.0
cycle/database                     2.7.1
cycle/migrations                   v3.1.2
cycle/orm                          v2.6.1
cycle/schema-builder               v2.7.0
cycle/schema-migrations-generator  2.0.0
cycle/schema-renderer              1.2.0

wapmorgan avatar Jan 23 '24 18:01 wapmorgan

Hi @wapmorgan I tried to reproduce your issue.

I created test entities:

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\BelongsTo;

#[Entity(table: 'users')]
class User
{
    #[Column(type: 'primary')]
    public int $id;

    #[BelongsTo(target: Voucher::class, nullable: true, fkAction: 'SET NULL')]
    public ?Voucher $voucher = null;
}
#[Entity(table: 'vouchers')]
class Voucher
{
    #[Column(type: 'primary')]
    public int $id;

    #[Column(type: 'string')]
    public string $title;
}

After that, I created the users table and set the foreign key for voucher_id with on delete SET NULL and on update CASCADE.

And then I ran the migration creation. As expected, the migration specifies 'SET NULL' for on delete and on update.

migration

Versions

php:8.3.0
postgresql 12
cycle/annotated                    v4.1.0
cycle/database                     2.9.0
cycle/migrations                   v4.2.3
cycle/orm                          v2.7.1
cycle/schema-builder               v2.8.0
cycle/schema-migrations-generator  2.2.0 
cycle/schema-renderer              1.2.0

msmakouz avatar Mar 29 '24 11:03 msmakouz