Sequelize 定义 n:m 关系(import 方式)

Posted Au_ww

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sequelize 定义 n:m 关系(import 方式)相关的知识,希望对你有一定的参考价值。

附上3张表的结构

albums 专题表

CREATE TABLE `albums` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) NOT NULL,
 `views` int(11) DEFAULT 0,
 `sort` int(11) DEFAULT 999999999,
 `status` varchar(255) DEFAULT off,
 `created_at` datetime NOT NULL,
 `updated_at` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

 products 产品表

CREATE TABLE `products` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) NOT NULL,
 `imgs` text NOT NULL,
 `before_price` double(12,2) NOT NULL,
 `after_price` double(12,2) DEFAULT NULL,
 `link` varchar(255) NOT NULL,
 `views` int(11) DEFAULT 0,
 `sort` int(11) DEFAULT 999999999,
 `status` varchar(255) DEFAULT off,
 `created_at` datetime NOT NULL,
 `updated_at` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

 album_product_mapping, mapping 表

CREATE TABLE `album_product_mappings` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `album_id` int(11) NOT NULL,
 `product_id` int(11) NOT NULL,
 `sort` int(11) NOT NULL DEFAULT 999999999,
 `created_at` datetime NOT NULL,
 `updated_at` datetime NOT NULL,
 PRIMARY KEY (`id`),
 KEY `album_id` (`album_id`),
 KEY `product_id` (`product_id`),
 CONSTRAINT `album_product_mappings_ibfk_1` FOREIGN KEY (`album_id`) REFERENCES `albums` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `album_product_mappings_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8

 

album 与 product 的关系为 n:m

 

在 sequelize 内定义 models

Album

‘use strict‘;

module.exports = function (sequelize, DataTypes) {
    var Album = sequelize.define(‘Album‘, {
        name: DataTypes.STRING,
        views: DataTypes.INTEGER,
        sort: DataTypes.INTEGER,
        status: DataTypes.STRING
    }, {
        underscored: true,
        tableName: "albums",
        classMethods: {
            associate: function (models) {
                Album.belongsToMany(models.Product, {
                    through: {
                        model: models.Album_product_mapping,
                        unique: false,
                    },
                    foreignKey: "album_id",
                    constraints: false,
                    as: "products"
                });
            }
        }
    });

    return Album;
};

 

Product

‘use strict‘;
module.exports = function (sequelize, DataTypes) {
    var Product = sequelize.define(‘Product‘, {
        name: DataTypes.STRING,
        imgs: DataTypes.TEXT,
        before_price: DataTypes.DOUBLE,
        after_price: DataTypes.DOUBLE,
        link: DataTypes.STRING,
        views: DataTypes.INTEGER,
        sort: DataTypes.INTEGER,
        status: DataTypes.STRING
    }, {
        underscored: true,
        tableName: "products",
        classMethods: {
            associate: function (models) {
                // associations can be defined here
                Product.belongsToMany(models.Album, {
                    through: {
                        model: models.Album_product_mapping,
                        unique: false
                    },
                    foreignKey: "product_id",
                    constraints: false
                })
            }
        }
    });

    return Product;
};

 

Album_product_mapping

‘use strict‘;
module.exports = function (sequelize, DataTypes) {
    var Album_product_mapping = sequelize.define(‘Album_product_mapping‘, {
        album_id: DataTypes.INTEGER,
        product_id: DataTypes.INTEGER,
        sort: DataTypes.INTEGER,
    }, {
        underscored: true,
        tableName: "album_product_mappings",
        classMethods: {
            associate: function (models) {
                // associations can be defined here
            }
        }
    });
    return Album_product_mapping;
};

关联关系的定义位于 classMethods 中。

 

以上是关于Sequelize 定义 n:m 关系(import 方式)的主要内容,如果未能解决你的问题,请参考以下文章

如何在 node.js 中使用 sequelize-npm 实现与连接表的一对多关系

使用 Sequelize.js 时在哪里定义关联/关系?

Sequelize:跨多个数据库的关系时“模型尚未定义”

使用 sequelize N:M 关联实现“平坦”结果的“正确”方法是啥

使用自定义连接表主键 Sequelize BelongsToMany

如何使用 sequelize 和 mysql 创建一对一的关系?