数组上的嵌套 FOREACH 仅在第一个数组值上执行

Nested FOREACH on array only executes on first array value

我正在编写一个函数来播种我的数据库数据。我做的第一件事是清除数据库中的所有内容:

export const clearDb = async () => {
  const deleteQuery = `
    MATCH (n)
    DETACH DELETE n
    `;
  await session.writeTransaction((tx) => tx.run(deleteQuery));
  console.log("done clearing DB!");
};

我的数据格式如下,保存为JavaScript变量:

type Temperature = "warm";

interface SeedData {
  alternativeNames: string[];
  name: string;
  hex: string;
  tags: string[];
  temperature: Temperature;
}

export const INITIAL_DATA: SeedData[] = [
  {
    name: "Butter",
    alternativeNames: ["Light Yellow", "Cream", "Lemon", "Daffodil", "Vanilla"],
    hex: "#ffff80",
    tags: [
      "cheerful",
      "pleasant",
      "gentle",
      "happy",
      "optimistic",
      "soft",
      "institutional",
    ],
    temperature: "warm",
  }
]

对于我的初始数据中的每个对象,我希望查询:

这是我的查询和调用它的函数:

export const seedColors = async () => {
  const writeQuery = `
    FOREACH (color in $colors |
        MERGE (c:Color {hex: color.hex})
            ON CREATE 
                SET c.hex = color.hex  
                SET c.name = color.name
                SET c.alternativeNames = color.alternativeNames
                SET c.temperature = color.temperature
            FOREACH (tag in color.tags | 
                MERGE(c)-[a:ASSOCIATED_WITH]->(t:Tag)
                    ON CREATE 
                        SET t.name = tag 
                ))
    RETURN $colors AS colors
    `;
  await session.writeTransaction((tx) =>
    tx.run(writeQuery, { colors: INITIAL_DATA })
  );
  console.log("done seeding data!");
};

然后我用这个函数调用所有东西:

export const seedData = async () => {
  try {
    await clearDb();
    await seedColors();
  } catch (e) {
    console.log(e);
    throw e;
  } finally {
    session.close();
  }
};

最终结果是我数据中的所有 :Color 节点都在数据库中创建,但每个节点只有一个 :TagASSOCIATED_WITH 关系。每个 :Color 仅创建一个 :Tag,而在给出的示例中,我希望有 7 个标签和 7 个关系。

我做错了什么?

我用两个 UNWIND 语句设法得到了想要的结果,但我仍然不知道为什么初始查询不起作用:

const writeQuery = `
    UNWIND $colors as color 
        MERGE (c:Color {hex: color.hex})
            ON CREATE 
                SET c.hex = color.hex  
                SET c.name = color.name
                SET c.alternativeNames = color.alternativeNames
                SET c.temperature = color.temperature
    WITH color, c UNWIND color.tags as tag 
                MERGE(c)-[a:ASSOCIATED_WITH]->(:Tag {name: tag})
    RETURN $colors AS colors
    `;