craftcms / craftcms/cms

[4.x]: Relational fields are cleared out when changing entry types

Open
#12,592 6 comments 0 reactions 1 assignee View on GitHub

@brianjhanson is already working on this.

Since Jan 30, 2023.

bug craft4
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

What happened?
Description

If the entry type is changed for an entry, relations made in a relational field that doesn't exist in the new entry type's field layout, are removed.

This is inconsistent with how Craft retains data for all other field types (plaintext, select fields, etc) when changing an entry's entry type. It's also not how this used to work (I'm not able to reproduce the behavior on Craft 3.7.63.1).

I'm unsure if the current behavior is an intentional change in Craft 4, or a bug?

One interesting detail: From looking at the relations table, it doesn't look like the relations are deleted when the entry type is first changed to the type without the relational field; it's only when the entry type is changed back to the type with the relational field, that the relations are actually deleted from the database. Don't know if that's relevant or not, but seemed a bit odd to me.

Steps to reproduce
  1. Create a section with two entry types, and add a relational field (assets, entries, Matrix ) to one of them.
  2. Create an entry and add an element to the relational field. Save the entry.
  3. Change the entry type to the one without the relational field. Save the entry.
  4. Change the entry type back to the type with the relational field. Notice that that the relational field is now empty.
Expected behavior

Existing relations made in fields that are no longer in an element's field layout after changing entry types should be retained, similar to how Craft retains data for all other field types in the same situation.

Actual behavior

Relations are deleted.

Craft CMS version

4.3.6.1

PHP version

8.1.12

Operating system and version

macOS 12.6

Database type and version

10.9.3-MariaDB

Image driver and version

No response

Installed plugins and versions

None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.