node.js - 如何给集合命名(当我们映射多对多关系时自动生成)?

标签 node.js sails.js relational-database relationship

我是 Sails js 的新手。我正在尝试客户和帐户关系(多对多)。 我能够在他们之间建立关系。当我插入第一条记录时创建的连接集合采用“modelname1_attributename_ modelname2_attributename”。

我们可以给它自定义名称,因为它会很容易进一步使用吗?

我来自 JAVA 和 Hibernate 背景,Hibernate 中有注释可以完成我想要的任务。

请帮我解决这个问题。

最佳答案

无法更改用于 Sails 多对多关联的自动生成的“through”表的名称。但是,您可以手动指定“直通”表,而不是使用自动生成的表,然后任意命名。来自Sails.js docs on through associations :

Many-to-Many through associations behave the same way as many-to-many associations with the exception of the join table being automatically created for you. In a Many-To-Many through assocation you define a model containing two fields that correspond to the two models you will be joining together. When defining an association you will add the through key to show that the model should be used rather than the automatic join table.

因此,在您的情况下,您需要创建一个新模型,例如api/models/CustomerAccount.js,例如:

module.exports = {
  attributes: {
    customer:{
      model:'customer'
    },
    account: {
      model: 'account'
    }
  }
}

然后在 Customer.jsAccount.js 模型文件中,在定义关联属性时使用 through 属性,例如在客户中:

accounts: {
  collection: 'customer',
  via: 'customers',
  through: 'customeraccount'
}

正如@zabware 在他的回答中指出的那样,您可以使用 tableName 进一步自定义此表的名称。

关于node.js - 如何给集合命名(当我们映射多对多关系时自动生成)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39848449/

相关文章:

node.js - 使用 Azure AD 登录 Nodejs Web 应用

javascript - 如何改变一个对象的各级键的值?

javascript - Uncaught Error : [$injector:modulerr] Failed to instantiate module toastr

Node JS中的JSON字符串化不序列化对象数组

mysql - 如何将json分解为sql

node.js - aws 上的 ETag 缓存

node.js - 使用 api.ai 和 heroku(node.js) 的 Facebook Messenger 聊天机器人中出现 OAuth 异常

sails.js - 如何在 Sails js 中使用 bigserial 作为主键 id

mysql - 如何获得最大匹配输出

mysql - 不计算 "LIMIT"中的某些列