对 Sequelize 关联感到困惑 [英] Confused about Sequelize associations

查看:18
本文介绍了对 Sequelize 关联感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现 Sequelize 是一个很好的 ORM 框架,可以在我的节点 + MySQL webapp 中使用.但是当我设计我的数据库表时,我对 Sequelize 关联感到非常困惑.

只需将 webapp 中的用户和系统通知视为一个非常简单的案例:

  • 该网站有很多用户
  • 网站会向部分或所有用户发送通知,并且相同的通知只是保存为一条记录
  • 用户可以知道他/她收到的任何通知是否已阅读(通过阅读日期)

然后我按预期设计了3张表:

  1. 用户:idname
  2. 通知:idtitlecontentsendAt
  3. Notify:idUserId(作为接收者)、NotificationIdreadAt

我可以通过 Sequelize 简单地定义 UserNotification 模型.但我只是不知道如何使用关联使 Notify 与 2 个表相关,通过外键 UserIdNotificationId.p>

我尝试过像这样使用 hasOne 关联:

Notify.hasOne(User);Notify.hasOne(通知);

然后在 UserNotification 表中都有一个 NotifyId 列.这不是我所期望的.可能是我想错了使用关联的方式,所以想知道如何正确使用?

另外,如果我想获得 JSON 格式的结果:

<代码>[{id: 123, user: [Object some user], notification: [Object some notification], readAt: null},{id: 124, user: [Object another user], notification: [Object some notification], readAt: "Mon Oct 29 2012 20:44:54 GMT+0800"}]

我怎样才能像以前使用的 SQL 一样只使用一次 find 方法查询:

选择Notify.id 为 'Notify.id',用户名作为用户名",Notification.title 为 'Notification.title',Notification.sendAt 作为 'Notification.sendAt',Notify.readAt 作为 'Notify.readAt'FROM 用户,通知,通知在哪里Notify.UserId = User.id ANDNotify.NotificationId = Notification.id;

解决方案

我找到了正确的方法.我意识到我的 Notify 表是一个完整的表,因为它包含一个 readAt 列.所以关联应该定义为:

Notification.hasMany(db.Notify);User.hasMany(db.Notify);

然后一切都变好了.

find中的第二个问题也已解决.像这样使用:

Notify.findAll({包括:['用户','通知']}).success(函数(通知){console.log(notify.user);console.log(notify.notification);});

I just found Sequelize as a good ORM framework to use in my node + MySQL webapp. But when I was designing my database tables, I got so confused about Sequelize associations.

Just think about user and system notification in a webapp as a very simple case:

  • The site has many users
  • The site would send notifications to some or every users, and the same notification just save as one record
  • Users could know any notification he/she received is read or not(by a read date)

Then I design 3 tables as expect:

  1. User: id, name
  2. Notification: id, title, content, sendAt
  3. Notify: id, UserId(as receiver), NotificationId, readAt

I can simply define User and Notification models by Sequelize. But I just don't know how to use associations to make Notify relate to the 2 tables, by foreign key UserId and NotificationId.

I have tried to use hasOne associations like this:

Notify.hasOne(User);
Notify.hasOne(Notification);

Then there comes a NotifyId column in both User and Notification tables. And this is not as my expect. Maybe I thought a wrong way to use associations, so I wanna know how to use it correctly?

Additionally, if I want to get results as the JSON:

[
    {id: 123, user: [Object some user], notification: [Object some notification], readAt: null},
    {id: 124, user: [Object another user], notification: [Object some notification], readAt: "Mon Oct 29 2012 20:44:54 GMT+0800"}
]

how can I use find method query just once like in a SQL I used before:

SELECT
    Notify.id as 'Notify.id',
    User.name as 'User.name',
    Notification.title as 'Notification.title',
    Notification.sendAt as 'Notification.sendAt',
    Notify.readAt as 'Notify.readAt'
FROM User, Notification, Notify
WHERE
    Notify.UserId = User.id AND
    Notify.NotificationId = Notification.id;

解决方案

And I found right way. I realized that my Notify table is a entiry table because containing a readAt column. So the association should be defined as:

Notification.hasMany(db.Notify);
User.hasMany(db.Notify);

then everything became OK.

The second problem in find has been resolved too. Just use like this:

Notify.findAll({
    include: ['User', 'Notification']
}).success(function (notify) {
    console.log(notify.user);
    console.log(notify.notification);
});

这篇关于对 Sequelize 关联感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆