Associations not working for Sequelize

落爺英雄遲暮 提交于 2019-12-13 06:16:10

问题


I run the below mutation first, creates the fish, I also created a river all ready. Why are rivers and fishes empty?

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
#     }
#   }
# }

The results are below:

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

Below are the two models:

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' });
};

Part of the graphql query:

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

Sequelize logs:

    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`)

EDIT

If I change my models the results are working from the River side of things:

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
  }

UPDATE

I believe this could be the issue. I have this in a my models directory:

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,
};

来源:https://stackoverflow.com/questions/50935562/associations-not-working-for-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!