我有一个名为 user
的模型和一个名为 access_barrier
的模型以及 user
和 access_barrier
之间的连接表> 称为 barrier_users
。
我为他们创建了以下关联
user.belongsToMany(models.access_barrier, {
as: "access_barriers_accessible",
through: "barrier_users",
foreignKey: "userId",
onUpdate: "CASCADE",
onDelete: "CASCADE"
});
access_barrier.belongsToMany(models.user, {
as: "users_with_permission",
through: "barrier_users",
foreignKey: "barrierId",
onUpdate: "CASCADE",
onDelete: "CASCADE"
});
当使用以下代码查询用户时,我可以看到所有数据都正常。
return models.user
.findAll({
include: [
{
model: models.access_barrier,
as: "access_barriers_accessible"
}
]
})
.then(function(result) {
resolve(result);
})
.catch(models.Sequelize.DatabaseError, function() {
reject("An error occurred in the database");
})
.catch(function(err) {
reject(err);
});
sequelize 的文档提到访问器方法是在使用上述功能时创建的,如下所述
Project.belongsToMany(User, {through: 'UserProject'});
User.belongsToMany(Project, {through: 'UserProject'});
This will create a new model called UserProject with the equivalent foreign keys projectId and userId. Whether the attributes are camelcase or not depends on the two models joined by the table (in this case User and Project).
Defining through is required. Sequelize would previously attempt to autogenerate names but that would not always lead to the most logical setups.
This will add methods getUsers, setUsers, addUser,addUsers to Project, and getProjects, setProjects, addProject, and addProjects to User.
我如何知道为我的模型添加的方法名称,我可以在哪里查看它们?我如何访问它们?
我使用了 Object.keys(models.user)
得到了下面的数据
[ 'sequelize',
'options',
'associations',
'underscored',
'tableName',
'_schema',
'_schemaDelimiter',
'rawAttributes',
'primaryKeys',
'_timestampAttributes',
'_readOnlyAttributes',
'_hasReadOnlyAttributes',
'_isReadOnlyAttribute',
'_dataTypeChanges',
'_dataTypeSanitizers',
'_booleanAttributes',
'_dateAttributes',
'_hstoreAttributes',
'_rangeAttributes',
'_jsonAttributes',
'_geometryAttributes',
'_virtualAttributes',
'_defaultValues',
'fieldRawAttributesMap',
'fieldAttributeMap',
'uniqueKeys',
'_hasBooleanAttributes',
'_isBooleanAttribute',
'_hasDateAttributes',
'_isDateAttribute',
'_hasHstoreAttributes',
'_isHstoreAttribute',
'_hasRangeAttributes',
'_isRangeAttribute',
'_hasJsonAttributes',
'_isJsonAttribute',
'_hasVirtualAttributes',
'_isVirtualAttribute',
'_hasGeometryAttributes',
'_isGeometryAttribute',
'_hasDefaultValues',
'attributes',
'tableAttributes',
'primaryKeyAttributes',
'primaryKeyAttribute',
'primaryKeyField',
'_hasPrimaryKeys',
'_isPrimaryKey',
'autoIncrementAttribute',
'_scope',
'_scopeNames',
'associate' ]
检查它是什么关联
{ access_barriers_incharge: access_barriers_incharge,
access_barriers_admin: access_barriers_admin,
admin: admin,
access_barriers_accessible: access_barriers_accessible,
barriers_accessed: barriers_accessed }
最佳答案
找到这篇文章,让我弄清楚什么魔法方法与 sequelize 对象相关联。
简而言之,您可以在 sequelize 对象上使用以下代码来获取其魔术方法。
console.log(Object.keys(sequelizeObject.__proto__));
关于node.js - 如何在 sequelize 中访问访问器方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52738883/