【发布时间】:2016-03-30 08:52:00
【问题描述】:
我正在尝试在 Postgresql 上创建这两个表的 Sequelize (3.20) 模型:
| user | | comment |
|-------| |----------|
| id | | id |
| email | | userId |
user 表在 crm 架构上,comment 表在 comment 架构上。 comment.userId 具有 user.id 的外键。
var User = db_conn.define('user', {
email: {
...
}
}, {
...
});
User.sync({force: true}).then(function () {
return User.create({
email: 'mail@gmail.com'
});
});
var Comment = db_conn.define('comment', {
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: User,
key : 'id',
deferrable: Sequelize.Deferrable.INITIALLY_DEFERRED
}
}
}, {
...
});
Comment.sync({force: true}).then(function () {
return Comment.create({
userId: 1,
});
});
但是我收到了这个错误:
Unhandled rejection SequelizeDatabaseError: relation "user" does not exist
生成的 SQL:
CREATE TABLE IF NOT EXISTS "comment"."comment" ("id" SERIAL, "userID" INTEGER NOT NULL REFERENCES "user" ("id") DEFERRABLE INITIALLY DEFERRED);
如您所见,它正在为 user 表而不是 'crm'.'user' 表创建 FK。这是因为公共模式上没有 user 表而导致错误。你能告诉我我错过了什么吗?
谢谢。
【问题讨论】:
标签: node.js postgresql sequelize.js