statamic-rad-pack / runway

Eloquently manage your database models in Statamic.
https://statamic.com/addons/rad-pack/runway
MIT License
113 stars 49 forks source link

HasMany relationship: unlink deletes related resource #582

Closed LukaBis closed 1 month ago

LukaBis commented 2 months ago

Description

I have 2 models: ArticleCategory and Article. Article category has many articles. In the article category blueprint I added HasMany fieldtype for that relationship. Adding article to article category works well, as expected, but when I try to unlink the article from article category the article gets deleted instead of just 'unlink'.

By 'unlink' I mean that article_category_id column should have value NULL. Also, it is worth mentioning that I implemented soft deletion for article so articles get soft deleted, not permanently deleted. (just in case it helps in detecting the problem)

ArticleCategory model has 'articles' eloquent relationship.

I provide you with both blueprints and migrations:

article blueprint

tabs:
  main:
    display: Hauptteil
    sections:
      -
        fields:
          -
            handle: unit_number
            field:
              type: text
              display: 'Unit no.'
              visibility: read_only
              width: 25
              listable: true
          -
            handle: article_number
            field:
              type: text
              display: 'Artikel Nr.'
              visibility: read_only
              width: 25
              listable: true
title: Article

article category blueprint

tabs:
  main:
    display: Hauptteil
    sections:
      -
        fields:
          -
            handle: title_de
            field:
              type: text
              display: 'Title (de)'
              validate:
                - required
                - 'new App\Domains\Articles\Rules\UniqueArticleCategoryValue'
          -
            handle: title_fr
            field:
              type: text
              display: 'Title (fr)'
              validate:
                - 'new App\Domains\Articles\Rules\UniqueArticleCategoryValue'
          -
            handle: articles
            field:
              resource: article
              create: false
              type: has_many
              display: Articles
title: 'Article category'

articles migration

Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('article_number')->unique();
            $table->string('unit_number');
            $table->foreignId('article_category_id')->nullable()->constrained('article_categories');
            $table->timestamps();
            $table->softDeletes();
        });

article categories migration

Schema::create('article_categories', function (Blueprint $table) {
            $table->id();
            $table->string('title_de')->unique();
            $table->string('title_fr')->nullable()->unique();
            $table->timestamps();
        });

Steps to reproduce

  1. create migrations and models: Article and ArticleCategory
  2. make them runway resources (add in runway config, in resources array), implement HasRunwayResource trait in models
  3. create blueprints (given above) for those resources
  4. add article to article category
  5. try to unlink the article from article category

Environment

Environment Application Name: Statamic Laravel Version: 11.19.0 PHP Version: 8.3.10 Composer Version: 2.7.7 Environment: local Debug Mode: ENABLED URL: localhost:8080 Maintenance Mode: OFF Timezone: Europe/Zurich Locale: de

Cache Config: NOT CACHED Events: NOT CACHED Routes: NOT CACHED Views: CACHED

Drivers Broadcasting: null Cache: file Database: sqlite Logs: stack / daily Mail: log Queue: sync Scout: collection Session: file

Locales Installed: de, en, fr LaravelLang\Lang\Plugin: 15.5.6 Locales Version: 2.9.2 Protected: de Publisher Version: 16.4.0

Sentry Enabled: MISSING DSN Environment: local Laravel SDK Version: 4.7.1 PHP SDK Version: 4.8.1 Release: NOT SET Sample Rate Errors: 100% Sample Rate Performance Monitoring: NOT SET Sample Rate Profiling: NOT SET Send Default PII: DISABLED

Statamic Addons: 3 Sites: 1 Stache Watcher: Disabled Static Caching: Disabled Version: 5.19.0 PRO

Statamic Addons statamic-rad-pack/runway: 7.7.3 statamic/eloquent-driver: 4.12.0 tv2regionerne/statamic-safeguard: 1.4.0

Statamic Eloquent Driver Asset Containers: file Assets: eloquent Blueprints: file Collection Trees: eloquent Collections: file Entries: eloquent Forms: file Global Sets: file Global Variables: eloquent Navigation Trees: eloquent Navigations: file Revisions: file Sites: file Taxonomies: file Terms: eloquent Tokens: eloquent

duncanmcclean commented 2 months ago

Runway has always deleted related models when "unlinking" them.

However, looking back at it now, it does seem like a bad decision. I'll have a think about better ways to handle it.

github-actions[bot] commented 1 month ago

Released as part of v7.9.0.