1. 程式人生 > >Sequelize 定義 n:m 關系(import 方式)

Sequelize 定義 n:m 關系(import 方式)

function cas data ping urn double lin fun class

附上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 方式)