postgresql 序列化急切加载标记Map关联

rxztt3cl  于 2023-03-12  发布在  PostgreSQL
关注(0)|答案(1)|浏览(110)

我正在尝试实现一个三表解决方案的项目/职位标签。

这些是我的模特

项目.模型.js

const Projects = db.sequelize.define (
    'Projects',
    {
        project_id: {
            type: Sequelize.DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            allowNull: false,
        },
        
        creator_id: {
            type: Sequelize.DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: User,
                key: 'id'
            }
        },

        project_title: {
            type: Sequelize.DataTypes.STRING,
            allowNull: false
        },

        project_description: {
            type: Sequelize.DataTypes.STRING,
            allowNull: false
        },

        creation_date: {
            type: Sequelize.DataTypes.DATE,
            allowNull: false,
            defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
        },

        last_updated_date: {
            type: Sequelize.DataTypes.DATE,
            allowNull: false,
            defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
        }
    }, {
    timestamps: false
    }
)

export default Projects

项目标签.模型.js

const ProjectTags = db.sequelize.define(
    'Project Tags',
    {
        id: {
            type: Sequelize.DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },

        project_id: {
            type: Sequelize.DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: Projects,
                key: 'project_id'
            }
        },

        project_tag_id: {
            type: Sequelize.DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: Tags,
                key: 'id'
            }
        },

    }, {
    timestamps: false
});
Tags.belongsToMany(Projects, { through: ProjectTags, foreignKey: 'project_id', as: 'tags' });
Projects.belongsToMany(Tags, { through: ProjectTags, foreignKey: 'project_tag_id', as: 'tags' });

export default ProjectTags

标签.模型.js

const Tags = db.sequelize.define(
    'Tags',
    {
        id: {
            type: Sequelize.DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },

        tag: {
            type: Sequelize.DataTypes.STRING,
            allowNull: false
        }
    }, {
    timestamps: false
});

export default Tags

我很想这样加载它:

const projects = Projects.findAll({
            attributes: ['project_id','creator_id','project_title','project_description','creation_date','last_updated_date'],
            include: [{
                model: Tags,
                as: 'tags',
                attributes: ['tag'],
            }]
        })
        .catch(error => {
            throw new Error(error);
        })
        return projects;

它很好地返回了项目数据,但是tags是一个空数组,尽管表不是空的并且引用看起来是正确的。

3htmauhk

3htmauhk1#

您混淆了关联中的外键选项。belongsToMany上的foreignKey选项应该指示链接您调用belongsToMany的主模型的字段,而不是您作为第一个参数传递的字段。当然,您需要指示不同的别名(它们应该对应于传递的模型(作为第一个参数)):

Tags.belongsToMany(Projects, { through: ProjectTags, foreignKey: 'project_tag_id', 
otherKey: 'project_id', as: 'projects' });
Projects.belongsToMany(Tags, { through: ProjectTags, foreignKey: 'project_id', otherKey: 'project_tag_id', as: 'tags' });

如果您想为传递的模型指定密钥,您需要使用foreignKey选项旁边的otherKey选项。

相关问题