是否可以在 postgreSQL 中将表 A ID 索引到表 B userId? 所以我会通过 id 或类似的东西加入 2 个表。
为了更好地解释我的问题,这里以 mongoDB 和 mongoose 为例:
const Billing = new Schema({
account:Number,
user: {
type: Schema.Types.ObjectId,
ref: 'user',
required: true
}
});
// later in the code i can do something like
Billing.findOne().populate('user');
这将在用户计费之间创建虚拟关系。
可以用 postgreSQL 做类似的事情吗
我正在使用 sequelize ORM。
最佳答案
听起来你在谈论外键 http://www.postgresql.org/docs/8.3/static/tutorial-fk.html
在你的情况下它应该是这样的:
create table B (
userid varchar(80) primary key,
<other fields>
);
create table A (
id varchar(80) references B(userid),
<other fields>
);
类型可以根据需要不同
关于javascript - 是否可以在 postgreSQL 中将表 A ID 索引到表 B userId?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36965077/