Skip to content

Seeds, support M2A relations. #159

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

Open
shadiramadan opened this issue Mar 21, 2025 · 0 comments
Open

Seeds, support M2A relations. #159

shadiramadan opened this issue Mar 21, 2025 · 0 comments

Comments

@shadiramadan
Copy link

shadiramadan commented Mar 21, 2025

The seeds feature is awesome! Thank you for your hard work @EdouardDem ! (Do you have a buy me a coffee link?)

I am, however, running into an issue for M2A relations. This might be a feature request if it's known it is unsupported.

The error itself is vague-

[09:26:46.786] ERROR (70683): Cannot read properties of null (reading 'startsWith')
    err: {
      "type": "TypeError",
      "message": "Cannot read properties of null (reading 'startsWith')",

But after adding a few console logs-
We see that it tries to call collection.startsWith on null (targetModel).

Logged in SeedDataMapper.initialize()

{ collection: 'nav_sections_items', field: 'item', targetModel: null }

This ends up returning null- because the line before it fails to retrieve the the related_collection from the relation field- because it doesn't have one.

 const targetModel = yield this.schemaClient.getTargetModel(this.collection, field);

The item field snapshot

{
  "collection": "nav_sections_items",
  "field": "item",
  "related_collection": null,
  "meta": {
    "junction_field": "nav_sections_id",
    "many_collection": "nav_sections_items",
    "many_field": "item",
    "one_allowed_collections": [
      "nav_sections",
      "links"
    ],
    "one_collection": null,
    "one_collection_field": "collection",
    "one_deselect_action": "nullify",
    "one_field": null,
    "sort_field": null
  }
}

The complexity here is that the targetModel of this item field has to be one of one_allowed_collections which comes from one_collection_field.

In my seeds example I added

targetModel ?? field === "item" && this.collection === "nav_sections_items" ? "links" : null

and seeded only one item type and that did end up working perfectly but that shouldn't be a static value. The targetModel could have multiple options.

So this line in data-mapper initialize:

this.idMappers[field] =
    this.idMapperClientFactory.forCollection(targetModel);

Needs to be dynamic based on the record's one_collection_field.

Versions:

  • Directus-Sync version: 3.2.3 (latest)
  • Directus version: 11.5.1 (latest)
  • Database: PostgreSQL
  • Directus environment: Docker/Self-hosted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant