第一次询问Stack Overflow问题所以我希望我正确地说出来:我正在尝试构建一个简单的博客应用程序作为家庭作业,同时使用Sequelize进行数据库管理.但是,当我尝试创建与我的模型关联的元素时,尝试创建一些testdata时遇到问题.这是我的代码,它应该与文档(
http://docs.sequelizejs.com/en/v3/docs/associations/#creating-elements-of-a-hasmany-or-belongstomany-association)中的语法相同.
当前行为:使用所有属性创建两个用户,根本不创建帖子并且不给出错误.
masterdb.sync({force:true}).then( x => {
return Promise.all([
User.create({
username:'Timothy',
password:'plain-text',
email:'x@msn.nl',
Posts: [
{ body: 'Hello everyone, my name is Timothy.' },
{ body: 'Today was a good day.'}
]
}, {
include: [ Post ]
}),
User.create({
username:'Jack Sparrow',
password:'plain-text',
email:'jacksparrow@msn.nl'
}),
])
}).catch(x => console.log(x))
以下是我的模型和关系声明:
const Sequelize = require('sequelize')
const masterdb = new Sequelize('blogapplication', process.env.POSTGRES_USER, process.env.POSTGRES_PASSWORD, {
dialect: 'postgres',
})
const User = masterdb.define('user', {
username: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
unique: true,
allowNull: false
}
}, {
paranoid: true
})
const Post = masterdb.define('post', {
body: {
type: Sequelize.STRING(9001),
allowNull: false,
unique:true,
}
}, {
paranoid: true
})
User.hasMany(Post)
最佳答案 摆弄你的代码和以下工作.我对Sequalize一无所知,所以不知道它是否应该像这样工作.但它的工作原理:)
posts: [
{ body: 'Hello everyone, my name is Timothy.' },
{ body: 'Today was a good day.'}
]
你有过:
Posts: [
{ body: 'Hello everyone, my name is Timothy.' },
{ body: 'Today was a good day.'}
]
你看得到差别吗?
我没有一段时间,但你有一个大写P,我有一个小写的p.
输出:
testdb=# select * from users;
id | username | password | email | createdAt | updatedAt | deletedAt
----+--------------+------------+--------------------+----------------------------+----------------------------+-----------
1 | Jack Sparrow | plain-text | jacksparrow@msn.nl | 2016-11-17 22:38:06.493+01 | 2016-11-17 22:38:06.493+01 |
2 | Timothy | plain-text | x@msn.nl | 2016-11-17 22:38:06.492+01 | 2016-11-17 22:38:06.492+01 |
(2 rows)
testdb=# select * from posts;
id | body | createdAt | updatedAt | deletedAt | userId
----+-------------------------------------+----------------------------+----------------------------+-----------+--------
1 | Hello everyone, my name is Timothy. | 2016-11-17 22:38:06.515+01 | 2016-11-17 22:38:06.515+01 | | 2
2 | Today was a good day. | 2016-11-17 22:38:06.515+01 | 2016-11-17 22:38:06.515+01 | | 2
(2 rows)
编辑:我想我知道为什么这必须是小写的.
您将帖子定义如下:
const Post = masterdb.define('post', {
如果你想以你的方式去做,你必须用大写字母P定义它.
我认为这是你问题的原因,因为我试图将帖子定义为Post,将密钥定义为post,然后它也不起作用.只有当定义和密钥相同时才有效.
奇怪的是,在docs of sequelize,他们确实像你一样有一个小写大写字母不规则.难道他们打错了吗?他们还用小写t而不是大写T定义“tag”.
我修改了你的代码,以便它“应该与他们的教程代码一起工作”,但事实并非如此.当我完成提议的更改时它确实有效 – 将定义中的小写字母更改为大写字母.但是,它们在定义标签的方式上确实有一些不同的方式.他们有var Tag = this.sequelize.definte(‘tag’,{.
而你需要var tag = masterdb.define(‘tag’,{.
难道他们的代码也会因为this.sequelize而起作用吗?
这是您的代码,其中包含其教程代码的工作版本.我将数据库重命名为testdb并对我更改的相关行发表评论:
const Sequelize = require('sequelize')
const masterdb = new Sequelize('testdb', process.env.POSTGRES_USER, process.env.POSTGRES_PASSWORD, {
dialect: 'postgres',
})
const User = masterdb.define('user', {
username: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
unique: true,
allowNull: false
}
}, {
paranoid: true
})
var Tag = masterdb.define('Tag', { //this is 'tag' in their tutorial
name: Sequelize.STRING
}, {
paranoid: true
})
User.hasMany(Tag)
masterdb.sync({force:true}).then( x => {
return Promise.all([
User.create({
username:'Timothy',
password:'plain-text',
email:'x@msn.nl',
Tags: [
{ name: 'Alpha'},
{ name: 'Beta'}
]
}, {
include: [ Tag ]
}),
User.create({
username:'Jack Sparrow',
password:'plain-text',
email:'jacksparrow@msn.nl'
}),
])
}).catch(x => console.log(x))