question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use deriveLinkedEntries with many references field needs documentation.

See original GitHub issue

Expected Behavior

When converting a single field to a many references field, one entry should be created and linked

Actual Behavior

Migration plan looks successful:

The following migration has been planned

Derive entries from articleContactBlock
  - from: emailAddress
  - to: address
  - via: emailAddresses
Update Content Type articleContactBlock

  Update field emailAddresses
    - disabled: false

Publish Content Type articleContactBlock
? Do you want to apply the migration Yes

Migration then fails:

 ❯ Derive entries from articleContactBlock
   ✖ Making requests (28/28)
   
   Update Content Type articleContactBlock
🚨  Migration unsuccessful
Batch failed

Error: {"status":"Unprocessable Entity","message":"Validation error","details":{"errors":[{"name":"type","value":{"sys":{"type":"Link","linkType":"Entry","id":"63dbf7d9-d48f-4aa0-98f9-bacaf6934a80"}},"type":"Array","details":"The type of \"value\" is incorrect, expected type: Array","path":["fields","emailAddresses","nl-NL"]}]},"url":"https://api.contentful.com:443/spaces/ea7q533w75xs/environments/migrate-test/entries/N7JP7I1Qwm8EAmesSQ8We"}

Error: {"status":"Conflict","message":"","details":{},"url":"https://api.contentful.com:443/spaces/ea7q533w75xs/environments/migrate-test/entries/N7JP7I1Qwm8EAmesSQ8We/published"}

....

Possible Solution

Create a variant of the derive entry example which shows how to use it with a many references field

Steps to Reproduce

run a migration similar to this:

module.exports = function (migration) {
    const emailAddress = migration.createContentType('emailAddress').name('Email adres');
    emailAddress.displayField('address');
    emailAddress.createField('address').type('Symbol').name('Adres').validations([{ unique: true }]);
    emailAddress.createField('label').type('Symbol').name('Label').validations([{ unique: true }]);
    emailAddress.createField('description').type('Symbol').name('Titel').validations([{ unique: true }]);
    emailAddress.createField('internal').type('Boolean').name('Voor intern gebruik');

    const articleContactBlock = migration.editContentType('articleContactBlock');

    articleContactBlock.editField('emailAddress').disabled(true);

    articleContactBlock.createField('emailAddresses')
        .name('Email adressen')
        .type('Array')
        .disabled(true)
        .items({
            type: 'Link',
            linkType: 'Entry',
            validations: [
                {
                    'linkContentType': [
                        'emailAddress'
                    ]
                }
            ]
        });

    articleContactBlock.moveField('emailAddresses').afterField('emailAddress');

    const sha1 = require('sha1');

    const articleContactBlock = migration.editContentType('articleContactBlock');
    migration.deriveLinkedEntries({
        contentType: 'articleContactBlock',
        derivedContentType: 'emailAddress',
        from: ['emailAddress'],
        toReferenceField: 'emailAddresses',
        derivedFields: [
            'address',
            'label',
            'description',
            'internal'
        ],
        identityKey: async (fromFields) => {
            if (typeof fromFields.emailAddress === 'undefined') {
                return 'empty';
            }

            return 'sha1' + sha1(fromFields.emailAddress);
        },
        shouldPublish: false,
        deriveEntryForLocale: async (inputFields, locale) => {
            if (typeof inputFields.emailAddress === 'undefined') {
                return {
                    'address': 'empty@example.org',
                    'label': '',
                    'description': '',
                    'internal': false
                }
            }

            return {
                'address': inputFields.emailAddress[locale],
                'label': '',
                'description': '',
                'internal': false
            };
        }
    });

    articleContactBlock.editField('emailAddresses').disabled(false);

    articleContactBlock.deleteField('emailAddress');
};

Context

We want to allow multiple entries instead of just one and are therefore trying to convert an existing field to many references.

Environment

  • Node Version: v9.11.2
  • Package Manager Version: yarn 1.5.1
  • Operating System: Linux 10a2d3449700 4.9.93-linuxkit-aufs
  • Package Version: Contentful CLI 0.16.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Khaledgarbayacommented, Aug 14, 2018

we’ll improve the tool in the future to detect field type and prevent this from happening.

1reaction
Khaledgarbayacommented, Oct 10, 2018

Hey @gkulasik-livongo we are working on some more improvement and we’ll update the cli soon

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to link one existing entry to another programmatically?
Hey all, I have a case where I need to link from one entry's reference field to another entry (JavaScript). const localeEntry =...
Read more >
Scripting migrations with the Contentful CLI
This tutorial details how to use the Contentful CLI to script changes to a content model and entries in a structured and reproducible...
Read more >
Migrating Markdown Tables to Rich Text in Contentful creating ...
The deriveLinkedEntries() wants to put the reference into a specific field but this is inline and can exist multiple times. I am able...
Read more >
contentful compose tutorial - Maxime Strobbe
You can only use the following field types with the order parameter: The ... Our documentation on multiple environments and continuous integration and ......
Read more >
Building a custom renderer for the reference field - YouTube
David joins Shy to chat about building a custom reference renderer using the App framework. They chat about the Contentful design system ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found