You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从MySQL迁移到Drupal时数据关联关系未保留

Drupal迁移:歌曲与作者关联关系映射失败修复

问题场景

将MySQL中的songs、author及关联桥接表song_author迁移至Drupal时,歌曲和作者数据已成功导入,但二者的关联关系未正确映射。当前使用的迁移YAML配置如下:

id: songs
label: Import Songs
migration_group: songs_migrate_many

migration_dependencies:
  required:
    - authors

source:
  plugin: table
  key: migrate_db
  table_name: songs
  id_fields:
    id:
      type: integer

process:
  title: title
  field_year: year

  field_authors:
    plugin: sub_process
    source: id
    process:
      target_id:
        plugin: migration_lookup
        migration: authors
        source:
          plugin: db_query
          key: migrate_db
          query: "SELECT author_id FROM song_author WHERE song_id = :id"
          placeholders:
            id: '@id'

destination:
  plugin: entity:node
  default_bundle: song

问题原因

原配置中sub_process的用法有误:sub_process需要接收数组类型的数据源,但当前传入的是单个歌曲ID;且在migration_lookup中嵌套使用db_query插件的方式不符合Drupal迁移插件的执行逻辑,无法正确获取当前歌曲对应的所有作者ID并完成映射。

修复方案

方案1:数据源阶段直接关联查询(推荐)

修改source部分,使用sql_query插件直接查询歌曲表与桥接表的关联数据,将每个歌曲对应的作者ID作为字段返回,后续直接通过migration_lookup完成映射:

id: songs
label: Import Songs
migration_group: songs_migrate_many

migration_dependencies:
  required:
    - authors

source:
  plugin: sql_query
  key: migrate_db
  # 关联查询歌曲与对应的作者ID
  query: |
    SELECT s.id, s.title, s.year, sa.author_id
    FROM songs s
    LEFT JOIN song_author sa ON s.id = sa.song_id
  # 以歌曲ID作为唯一标识
  id_fields:
    id:
      type: integer
  # 统计总迁移行数
  row_count_query: SELECT COUNT(*) FROM songs

process:
  title: title
  field_year: year

  # 映射作者关联字段,开启多值支持
  field_authors:
    plugin: migration_lookup
    migration: authors
    source: author_id
    multiple: true

destination:
  plugin: entity:node
  default_bundle: song

方案2:处理阶段动态查询关联作者ID

如果无法修改数据源查询,可通过callback插件动态获取当前歌曲对应的所有作者ID,再逐个完成迁移映射:

id: songs
label: Import Songs
migration_group: songs_migrate_many

migration_dependencies:
  required:
    - authors

source:
  plugin: table
  key: migrate_db
  table_name: songs
  id_fields:
    id:
      type: integer

process:
  title: title
  field_year: year

  field_authors:
    # 1. 获取迁移数据库连接
    - plugin: callback
      callable: Drupal\Core\Database\Database::getConnection
      arguments: ['migrate_db']
    # 2. 执行查询获取当前歌曲的所有作者ID
    - plugin: callback
      callable: 'query'
      arguments:
        - "SELECT author_id FROM song_author WHERE song_id = :id"
        - { id: '@id' }
    # 3. 将查询结果转为作者ID数组
    - plugin: callback
      callable: 'fetchCol'
    # 4. 遍历每个作者ID,映射到Drupal作者节点ID
    - plugin: sub_process
      process:
        target_id:
          plugin: migration_lookup
          migration: authors
          source: '@value'

destination:
  plugin: entity:node
  default_bundle: song

注意事项

  • 确保authors迁移已成功完成(依赖已满足),否则migration_lookup无法找到对应作者的节点ID。
  • 确认Drupal中song内容类型的field_authors字段为多值引用字段(引用author内容类型),否则无法存储多个作者关联。

内容的提问来源于stack exchange,提问作者imtinan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 16:20:11