gpt4 book ai didi

javascript - twitter 中 Sequelize 中的多对多 self 引用,如 web-app

转载 作者:行者123 更新时间:2023-12-03 22:31:52 24 4
gpt4 key购买 nike

我正在尝试构建一个博客网络应用程序,作为了解 ORM 和 Sequelize 的练习。我很容易地在帖子和评论以及用户和帖子之间建立了关系。没有问题。当我尝试将用户与用户作为关注者以及用户与用户作为被关注者进行链接时,恐怖就开始了。我也希望可以跟随自己。

我试过这样的事情:

  const User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
activated: {
type: Sequelize.BOOLEAN
}
})
User.belongsToMany(User, {as: "Follower", foreignKey: "FollowerId", through: "Follower_Followeds"})
User.belongsToMany(User, {as: "Followed", foreignKey: "FollowedId", through: "Follower_Followeds"})

并设置关系:
app.post("/followhandler", function(req, res) {
let userId = req.session.userId
let followId = req.body.followId

User.findById(userId)
.then( currentUser => {
User.findById(followId)
.then( follows => {
currentUser.addUser(follows)
})
})
.catch(e => console.log(e))
})

但是 Follower_Followeds 表不会得到更新。
我还尝试通过创建一个单独的表并手动添加关系来跳过 Sequelize 的多对多实现:
const FollowerFollowed = sequelize.define('followerFollowed', {
followedId: {
type: Sequelize.INTEGER
},

followerId: {
type: Sequelize.INTEGER
}
})

//define Users
const User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
activated: {
type: Sequelize.BOOLEAN
}
})

和处理程序:
app.post("/followhandler", function(req, res) {
let userId = req.session.userId
let followId = req.body.followId

FollowerFollowed.create( {
followerId: userId,
followedId: followId
})
.then( rel => console.log(rel))
.catch(e => console.log(e))
})

但在这种情况下我收到以下错误:

TypeError: val.replace is not a function at Object.SqlString.escape (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/sql-string.js:61:15) at Object.escape (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/dialects/abstract/query-generator.js:978:22) at Object.insertQuery (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/dialects/abstract/query-generator.js:299:28) at QueryInterface.insert (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/query-interface.js:497:33) at . (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/instance.js:679:56) at tryCatcher (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:510:31) at Promise._settlePromise (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:567:18) at Promise._settlePromise0 (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:612:10) at Promise._settlePromises (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:691:18) at Async._drainQueue (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:649:20) at tryOnImmediate (timers.js:622:5) at processImmediate [as _immediateCallback] (timers.js:594:5) TypeError: val.replace is not a function at Object.SqlString.escape (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/sql-string.js:61:15) at Object.escape (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/dialects/abstract/query-generator.js:978:22) at Object.insertQuery (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/dialects/abstract/query-generator.js:299:28) at QueryInterface.insert (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/query-interface.js:497:33) at . (/home/piepongwong/Documents/NYCDA/blog/node_modules/sequelize/lib/instance.js:679:56) at tryCatcher (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:510:31) at Promise._settlePromise (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:567:18) at Promise._settlePromise0 (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:612:10) at Promise._settlePromises (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/promise.js:691:18) at Async._drainQueue (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/piepongwong/Documents/NYCDA/blog/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:649:20) at tryOnImmediate (timers.js:622:5) at processImmediate [as _immediateCallback] (timers.js:594:5)

最佳答案

创建 M:M 关联时,您使用 as: 'Follower'as: 'Followed' 属性。它会影响您在用户之间添加/设置/删除关联的方式。将关注的用户添加到其他用户的方法现在称为 addFollowed 并且将按照与您完全相同的方式使用。

User.findById(userId).then( currentUser => {
User.findById(followId).then( follows => {
currentUser.addFollowed(follows); // notice the difference here
});
});

如果您想向给定用户添加关注者,也会发生同样的情况 - 那么您将使用 addFollower 方法。

此外,在您的第二个解决方案中,您在创建关联模型时犯了一个错误。在 User 模型中,您通过外键 FollowerIdFollowedId 定义了关联,但是在 FollowerFolloweds 定义中,您创建了 followerIdfollowedId ,因此这两个不匹配 - 您需要保持名称一致。

编辑

根据 belongsToMany 关系中的文档, as 属性应命名为复数,因此在您的情况下,您应该将它们命名为 as: 'Followers'as: 'Followeds'(第二个似乎有点奇怪)。 Sequelize 将这些值本身单一化,但是您可以使用对象 as: { singular: 'Follower', plural: 'Followers' } 自己定义它

The alias of this association. If you provide a string, it should be plural, and will be singularized using node.inflection. If you want to control the singular version yourself, provide an object with plural and singular keys.

关于javascript - twitter 中 Sequelize 中的多对多 self 引用,如 web-app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66217040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com