javascript - 是否可以在 postgreSQL 中将表 A ID 索引到表 B userId?

标签 javascript node.js postgresql sequelize.js

是否可以在 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/

相关文章:

javascript - 子字符串方法不返回最大字符限制

node.js - 类型错误 : next is not a function when io. 使用(passport.authenticate ('google' ))

javascript - express-session secret 应该是静态的还是动态的?

php - 使用 'and' 或 'or' 作为变量在 PHP 中查询

ruby-on-rails - 如何使用单个 SQL 查询从 activerecord 中高效地检索对象组?

javascript - 如何临时修改元素的CSS

javascript - 形成多个复选框,限制可以选择的数量

node.js - MVC 框架中的 Controller ,用于没有 View 的 REST API

postgresql - PostgreSQL 错误 : right sibling's left-link doesn't match: block 5 links to 8 instead of expected 2 in index "pg_toast_2619_index"

javascript - Angular Observables 返回匿名函数