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

🐛 fkAction/fkOnDelete has no effect for automatically created migrations #458

Open
1 task done
wapmorgan opened this issue Jan 23, 2024 · 1 comment
Open
1 task done
Assignees
Labels
status:to be verified Needs to be reproduced and validated. status:under discussion

Comments

@wapmorgan
Copy link

No duplicates 🥲.

  • 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 wapmorgan added status:to be verified Needs to be reproduced and validated. type:bug Bug labels Jan 23, 2024
@wapmorgan wapmorgan changed the title 🐛 fkAction/fkOnDelete has no effect 🐛 fkAction/fkOnDelete has no effect for automatically created migrations Jan 23, 2024
@msmakouz
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated. status:under discussion
Projects
Status: Todo
Development

No branches or pull requests

2 participants