如何在我的模型中定义 mysql 中另一个表的 id 数组并进行续集

发布于 2025-01-10 21:47:28 字数 2004 浏览 0 评论 0原文

我在express.js、node.js、mysql和sequelize中创建了一个api。

我有两个模型:自定义字段和客户类型

这是我的客户类型模型:

module.exports = (sequelize, DataTypes) => {

    const CustomerType = sequelize.define("customerType", {
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        description: {
            type: DataTypes.STRING,
            allowNull: false
        },
        user_id: {
            type: DataTypes.STRING,
            allowNull: false
        }
    })

    return CustomerType
}

还有我的自定义字段模型:

const customerType = require('./customerTypeModel')

module.exports = (sequelize, DataTypes) => {

    const CustomField = sequelize.define("customField", {
        customer_type_ids: [
            {
                type: DataTypes.INTEGER,
                references: {
                    model: customerType,
                    key: "id"
                }
            }
        ],
        user_id: {
            type: DataTypes.STRING,
            allowNull: false
        },
        system_name: {
            type: DataTypes.STRING,
            allowNull: true
        },
        title: {
            type: DataTypes.STRING,
            allowNull: true
        },
        type: {
            type: DataTypes.STRING,
            allowNull: true
        },
        required: {
            type: DataTypes.BOOLEAN,
            allowNull: true
        },
    })
    return CustomField
}

我需要在我的 customField 表中包含来自 customerType 表的 id 数组。 例如,我需要获取这样的 json:

{
 "id": 1
 "customer_types_ids": [ 
   {
     "id": 1,
     "name": "Customers",
     "description": "Customers",
     "user_id": "123456"
   },
   {
     "id": 2,
     "name": "Leads",
     "description": "Leads",
     "user_id": "123456"
   }
],
 "user_id": "123456",
 "system_name": "firstname",
 "title": "Firstname",
 "type": "text",
 "required": true
}

我应该如何构建我的自定义字段模型才能获得这样的 json 作为回报?

感谢您的帮助 !

I create an api in express.js, node.js, mysql and sequelize.

I have two models : Custom fields and Customer Types

Here is my customer type model :

module.exports = (sequelize, DataTypes) => {

    const CustomerType = sequelize.define("customerType", {
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        description: {
            type: DataTypes.STRING,
            allowNull: false
        },
        user_id: {
            type: DataTypes.STRING,
            allowNull: false
        }
    })

    return CustomerType
}

And there is my custom fields model :

const customerType = require('./customerTypeModel')

module.exports = (sequelize, DataTypes) => {

    const CustomField = sequelize.define("customField", {
        customer_type_ids: [
            {
                type: DataTypes.INTEGER,
                references: {
                    model: customerType,
                    key: "id"
                }
            }
        ],
        user_id: {
            type: DataTypes.STRING,
            allowNull: false
        },
        system_name: {
            type: DataTypes.STRING,
            allowNull: true
        },
        title: {
            type: DataTypes.STRING,
            allowNull: true
        },
        type: {
            type: DataTypes.STRING,
            allowNull: true
        },
        required: {
            type: DataTypes.BOOLEAN,
            allowNull: true
        },
    })
    return CustomField
}

I need to have in my customField table an array of id from the customerType table.
For exemple i need to get a json like this :

{
 "id": 1
 "customer_types_ids": [ 
   {
     "id": 1,
     "name": "Customers",
     "description": "Customers",
     "user_id": "123456"
   },
   {
     "id": 2,
     "name": "Leads",
     "description": "Leads",
     "user_id": "123456"
   }
],
 "user_id": "123456",
 "system_name": "firstname",
 "title": "Firstname",
 "type": "text",
 "required": true
}

How should I structure my custom field Model to be able to have a json like this in return?

Thanks for your help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆依然 2025-01-17 21:47:28

我假设您期望 CustomFieldCustomerType 之间存在一对多关系。在本例中,CustomerType 拥有该关系,因此在 CustomerType 的关系表中存在对 CustomField 的引用,称为 customFieldId代码>.

// customerType.js
module.exports = (sequelize, DataTypes) => {
    const CustomerType = sequelize.define("customerType", {
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        description: {
            type: DataTypes.STRING,
            allowNull: false
        },
        user_id: {
            type: DataTypes.STRING,
            allowNull: false
        }
    });

    return CustomerType;
}

// customField.js
module.exports = (sequelize, DataTypes) => {
    const CustomField = sequelize.define("customField", {
        user_id: {
            type: DataTypes.STRING,
            allowNull: false
        },
        system_name: {
            type: DataTypes.STRING,
            allowNull: true
        },
        title: {
            type: DataTypes.STRING,
            allowNull: true
        },
        type: {
            type: DataTypes.STRING,
            allowNull: true
        },
        required: {
            type: DataTypes.BOOLEAN,
            allowNull: true
        },
    });

    return CustomField;
}

// db.js
...
const CustomerType = require("./customerType.js")(sequelize, Sequelize.DataTypes);
const CustomField = require("./customField.js")(sequelize, Sequelize.DataTypes);

CustomField.hasMany(CustomerType, { as: "customer_types_ids" });
CustomerType.belongsTo(CustomField, {
    foreignKey: "id",
    as: "customFieldId",
});

这应该定义两个具有一对多关系的关系表。以下代码可用于加载 CustomField 及其所有 CustomerType

CustomField.findByPk(id, { include: ["customer_types_ids"] });

I assume you're expecting a One-to-Many relationship between CustomField and CustomerType. In this case, CustomerType owns the relation, so there is a reference to CustomField in the relational table of CustomerType called customFieldId.

// customerType.js
module.exports = (sequelize, DataTypes) => {
    const CustomerType = sequelize.define("customerType", {
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        description: {
            type: DataTypes.STRING,
            allowNull: false
        },
        user_id: {
            type: DataTypes.STRING,
            allowNull: false
        }
    });

    return CustomerType;
}

// customField.js
module.exports = (sequelize, DataTypes) => {
    const CustomField = sequelize.define("customField", {
        user_id: {
            type: DataTypes.STRING,
            allowNull: false
        },
        system_name: {
            type: DataTypes.STRING,
            allowNull: true
        },
        title: {
            type: DataTypes.STRING,
            allowNull: true
        },
        type: {
            type: DataTypes.STRING,
            allowNull: true
        },
        required: {
            type: DataTypes.BOOLEAN,
            allowNull: true
        },
    });

    return CustomField;
}

// db.js
...
const CustomerType = require("./customerType.js")(sequelize, Sequelize.DataTypes);
const CustomField = require("./customField.js")(sequelize, Sequelize.DataTypes);

CustomField.hasMany(CustomerType, { as: "customer_types_ids" });
CustomerType.belongsTo(CustomField, {
    foreignKey: "id",
    as: "customFieldId",
});

This should define two relational tables with a One-to-Many relationship. The following code can be used to load a CustomField with all its CustomerTypes.

CustomField.findByPk(id, { include: ["customer_types_ids"] });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文