How to copy collection to another branch?

I have been playing with data migrations but I can’t figure out how to copy a collection to another branch. I tried a simple test where I use the autogenerate migration script and it give errors.

For example here I am running autogenerated script for a migration from one branch to another::
$ dato migrate --source=example --inPlace --token=
Migrations will be run in sandbox env example

  • Running 1669455966_addTestWithData.js…
    Create new models/block models
    Create model “Test” (test)
    Warning: Attribute api_key should be expressed in camelCase syntax (apiKey)
    Warning: Attribute all_locales_required should be expressed in camelCase syntax (allLocalesRequired)
    Warning: Attribute collection_appearance should be expressed in camelCase syntax (collectionAppearance)
    Creating new fields/fieldsets
    Create Single-line string field “Name” (name) in model “Test” (test)
    Warning: Attribute field_type should be expressed in camelCase syntax (fieldType)
    Warning: Attribute api_key should be expressed in camelCase syntax (apiKey)
    Warning: Attribute default_value should be expressed in camelCase syntax (defaultValue)

Command failed!

Failed request:

POST https://site-api.datocms.com/item-types/[object Object]/fields

content-type: application/json
accept: application/json
authorization: Bearer token
user-agent: js-client v3.5.21
X-Api-Version: 3
x-environment: example

{“data”:{“type”:“field”,“attributes”:{“label”:“Name”,“field_type”:“string”,“api_key”:“name”,“appearance”:{“addons”:[],“editor”:“single_line”,“parameters”:{“heading”:false}},“default_value”:“”}}}

HTTP Response:

404 Not Found

Anyway, any links to working code to simply copy a collection and it’s data to another branch? Thanks.

Hello @jterry and welcome to the community!

Keep in mind that the auto-generation of migration scripts only supports schema changes at the moment (models and their fields) and not actual content (the records created from the models) so attempting to use auto-migration for that can generate some errors.

For content migration you would have to manually write the script, using the operations listed on our REST API: Overview - DatoCMS

If you run into any issues on writing that script, please send us an email at support@datocms.com with your project link and the model + content you wish to migrate and we can help you create a script for its migration.

I need an actual clone of the data and all the id’s…if I write it manually it won’t maintain the ids and will be different. It seems like there should be an easier way to just transfer a model and it’s content to another branch. I’ll play with it though.

@jterry i don’t mean manually type in the IDs of records, just the logic:
You can use this call to get all records dynamically without having to hardcode any IDs: List all records - Record - Content Management API
And this call to the create new records based on the fetched information:
Create a new record - Record - Content Management API