sequelize中间表多态外键

const Sequelize =require('sequelize')
const sequelize = new Sequelize(
	'testdatabase', 
	'root', 
	'216612',
	{
        'dialect': 'mysql',  // 数据库使用mysql
        'host': 'localhost', // 数据库服务器ip
        'port': 3306,        // 数据库服务器端口
        'define': {
            // 字段以下划线(_)来分割(默认是驼峰命名风格)
            'underscored': true
        }
    });

const ItemTag = sequelize.define('item_tag', {
    id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
    tag_id: {
        type: Sequelize.INTEGER,
        unique: 'item_tag_taggable'
    },
    taggable: {
        type: Sequelize.STRING,
        unique: 'item_tag_taggable'
    },
    taggable_id: {
        type: Sequelize.INTEGER,
        unique: 'item_tag_taggable',
        references: null
    }
});
const Tag = sequelize.define('tag', {
    name: Sequelize.STRING
});
const Post = sequelize.define('post', {
    name: Sequelize.STRING
});
const Image = sequelize.define('image', {
    name: Sequelize.STRING
});

Post.belongsToMany(Tag, {
    through: {
        model: ItemTag,
        unique: false,
        scope: {
            taggable: 'post'
        }
    },
    foreignKey: 'taggable_id',
    constraints: false
});
Image.belongsToMany(Tag, {
    through: {
        model: ItemTag,
        unique: false,
        scope: {
            taggable: 'image'
        }
    },
    foreignKey: 'taggable_id',
    constraints: false
});
Tag.belongsToMany(Post, {
    through: {
        model: ItemTag,
        unique: false
    },
    foreignKey: 'tag_id',
    constraints: false
});
Tag.belongsToMany(Image, {
    through: {
        model: ItemTag,
        unique: false
    },
    foreignKey: 'tag_id',
    constraints: false 
});

let addTag=async ({name})=>{
	let tag=await Tag.create({name})
	return tag
}
let addPost=async ({name}) => {
	let post=await Post.create({name})
	return post
}
let addImage=async ({name})=>{
	let image=await Image.create({name})
	return image
}


	


sequelize.sync({force: true}).then(async function() {
	let tag1=await addTag({name:"normal"})
	let post1=await addPost({name:"post"})
	let post2=await addPost({name:"post"})
	let image1=await addImage({name:"image"})
	let image2=await addImage({name:"image"})
	//await tag1.addPost(post1,{through:{taggable:"post"}})
	await post1.addTag(tag1)
	//await tag1.addImage(image1,{through:{taggable:"image"}})
	await image1.addTag(tag1)
	//await tag1.addPost(post2,{through:{taggable:"post"}})
	await post2.addTag(tag1)
	//await tag1.addImage(image2,{through:{taggable:"image"}})
	await image2.addTag(tag1)



    return tag1
}).then(function(jane) {
    console.log(jane.get({
        plain: true
    }));
});
中间表ItemTag中多列unique设置为一个相同字符串可以组成一个联合唯一约束; 
 
中间表要设置主键自增id;

constraints: false禁用taggable_id上的引用约束因为该列是多态的,所以我们不能说它是REFERENCES一个特定的表;

添加数据时使用

await post1.addTag(tag1)
而不是
await tag1.addPost(post1,{through:{taggable:"post"}})

否则

taggable_id列相同的后面会覆盖前面;

使用sequelize.sync({force: true})初始化数据库

猜你喜欢

转载自blog.csdn.net/qq_35640577/article/details/80048596