不为Sequelize工作的协会

时间:2018-06-19 19:21:54

标签: node.js reactjs express sequelize.js graphql

我首先运行以下突变,创建了鱼,还创建了一条准备就绪的河。为什么是空的?

Graphql:

query {
  fishes {
    name
    species
    rivers {
      name
    }
  }
  river(id: 1) {
    name
    fishes { 
    name}
  }
}

# mutation {
#   createFish (name: "Orange-Vall Smallmouth Yellowfish", species: "Labeobarbus aeneus", riverId: 1) {
#     name
#     rivers {
#       name
#     }
#   }
# }

结果如下:

{
  "data": {
    "fishes": [
      {
        "name": "Orange-Vall Smallmouth Yellowfish",
        "species": "Labeobarbus aeneus",
        "rivers": []
      }
    ],
    "river": {
      "name": "Orange River",
      "fishes": []
    }
  }
}

下面是两个模型:

const Fish = sequelize.define('fish', schema);
Fish.associate = models => {
  Fish.belongsToMany(models.River, { through: 'RiverFish' });
};

const River = sequelize.define('river', schema);
River.associate = models => {
  River.belongsToMany(models.Fish, { through: 'RiverFish', as: 'Fishes' });
};

graphql查询的一部分:

River: {
    fishes: river => river.getFishes(),   },   
Fish: {
    rivers: fish => fish.getRivers(),   },

序列化日志:

    Executing (default): CREATE TABLE IF NOT EXISTS `fishes` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255), `species` VARCHAR(255) DEFAULT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Connection has been established successfully.
Executing (default): PRAGMA INDEX_LIST(`fishes`)
Executing (default): CREATE TABLE IF NOT EXISTS `rivers` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255), `alternative` VARCHAR(255) DEFAULT NULL, `geojson` JSON, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`rivers`)
Executing (default): CREATE TABLE IF NOT EXISTS `RiverFish` (`createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `fishId` INTEGER NOT NULL REFERENCES `fishes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, `riverId` INTEGER NOT NULL REFERENCES `rivers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (`fishId`, `riverId`));
Executing (default): PRAGMA INDEX_LIST(`RiverFish`)
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_RiverFish_1`)

编辑

如果我更改模型,则结果将在River方面起作用:

River.hasMany(models.Fish, { as: 'Fishes' });
Fish.belongsToMany(models.River, { through: 'RiverFish', as: 'Rivers' });

{
  "data": {
    "fishes": [
      {
        "id": "1",
        "name": "Orange-Vall Smallmouth Yellowfish",
        "species": "Labeobarbus aeneus",
        "rivers": []
      }
    ],
    "river": {
      "name": "Orange River",
      "fishes": [
        {
          "name": "Orange-Vall Smallmouth Yellowfish"
        }
      ]
    },
    "rivers": null
  }

更新

我相信这可能是问题所在。我的模型目录中有这个文件:

import Fish from './Fish';
import River from "./River";

const models = {
    Fish,
    River
};

Object.keys(models).forEach(name => {
    const model = models[name];
    if (typeof model.associate === 'function') {
        model.associate(models);
    }
});

export {
    Fish, River,
};

0 个答案:

没有答案