Sequelize关联无需查询即可获得计数

时间:2017-05-31 09:47:41

标签: javascript node.js postgresql sequelize.js

我有两个模型:

类型

class TrayIcon(QSystemTrayIcon):
    """
    Displays a system tray icon
    """

    def __init__(self, interface: Interface) -> None:
        """
        Constructor
        :param interface: Interface to show when the tray icon is clicked
        """
        super().__init__(QIcon(resource_filename("ezstorage.resources.img.tray_icon", "folder.png")))
        self.interface = interface
        self.setVisible(True)
        self.show()
        self.activated.connect(self.clicked)
        menu = QMenu()
        action = QAction("Exit")
        menu.addAction(action)
        self.setContextMenu(menu)

一个植物可以只有1种类型,但是1种类型可以与许多植物相关联,所以在我的客户端应用程序中我有类似树的东西,我点击1个家庭它给我看了很多类型,我点击1它向我展示了所有植物。

我需要的是:当我在家庭中进入时,我想要显示每种类型的植物数量,所以我需要以某种方式知道我的类型中植物的长度,此刻我可以获取与流派视图内的流派相关的数据。

所以我执行此查询:

    module.exports = function (sequelize, DataTypes) {
      var Genre = sequelize.define("Genre", {
        name: {
          type: DataTypes.STRING,
          allowNull: false
        },
         familyId: {
          type: DataTypes.INTEGER,
          allowNull: true
        }
      },
        {
          associate: function (models) {
            Genre.hasMany(models.Plant, { foreignKey: "genreId", as: 'plant' });
          }
        }
      );

**Plant**

module.exports = function (sequelize, DataTypes) {
  var Plant = sequelize.define("Plant", {
    specie: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      defaultValue: "No description for this plant yet"
    },
    directory: {
      type: DataTypes.STRING,
      allowNull: false
    },
     genreId: {
      type: DataTypes.INTEGER,
      allowNull: true
    }
  },
    {
      associate: function (models) {
        Plant.hasMany(models.Foto, { foreignKey: "plantId", as: 'fotos' });
      }
    }
  );

我把特别包括在那里,所以我可以得到所有的植物,但它不起作用我需要得到这个查询中的植物数量,

谢谢

0 个答案:

没有答案
相关问题