博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sequelize-nodejs-13-Working with legacy tables
阅读量:7077 次
发布时间:2019-06-28

本文共 1469 字,大约阅读时间需要 4 分钟。

Working with legacy tables使用遗留表

While out of the box Sequelize will seem a bit opinionated it's trivial to both legacy and forward proof your application by defining (otherwise generated) table and field names.

虽然开箱即用的Sequelize会显得有点固执己见,但是可以通过定义(否则生成)表和字段名来使用你的应用的遗留和之前的凭据,这是微不足道的。

 

Tables表

sequelize.define('user', {}, {  tableName: 'users'});

 

Fields字段

sequelize.define('modelName', {  userId: {    type: Sequelize.INTEGER,    field: 'user_id'  }});

 

Primary keys主键

Sequelize will assume your table has a id primary key property by default.

Sequelize将假设您的表默认具有id主键属性

To define your own primary key:

想要定义你自己的主键:

sequelize.define('collection', {  uid: {    type: Sequelize.INTEGER,    primaryKey: true,    autoIncrement: true // Automatically gets converted to SERIAL for postgres  }});sequelize.define('collection', {  uuid: {    type: Sequelize.UUID,    primaryKey: true  }});

And if your model has no primary key at all you can use Model.removeAttribute('id');

如果你的模型根本没有主键,你可以使用 Model.removeAttribute('id');

 

Foreign keys外键

// 1:1Organization.belongsTo(User, {foreignKey: 'owner_id'});User.hasOne(Organization, {foreignKey: 'owner_id'});// 1:MProject.hasMany(Task, {foreignKey: 'tasks_pk'});Task.belongsTo(Project, {foreignKey: 'tasks_pk'});// N:MUser.hasMany(Role, {through: 'user_has_roles', foreignKey: 'user_role_user_id'});Role.hasMany(User, {through: 'user_has_roles', foreignKey: 'roles_identifier'});

 

 

转载于:https://www.cnblogs.com/wanghui-garcia/p/10071427.html

你可能感兴趣的文章
Mysql完全备份
查看>>
使用Java窃取sina大片
查看>>
榕树下单身作者大征募!
查看>>
SQL Server T-SQL高级查询
查看>>
Python自省(反射)指南[转]
查看>>
阿里巴巴公布了一份最新的AI成绩单
查看>>
TableCell自适应表格高度
查看>>
Azure ARM (4) 开始创建ARM Resource Group并创建存储账户
查看>>
jQuery学习笔记开篇
查看>>
基于Xcode5的本地化
查看>>
【Linux高级驱动】linux设备驱动模型之平台设备驱动机制【转】
查看>>
Windows Embedded Standard开发初体验(一)
查看>>
与二进制有关的几道面试题
查看>>
这样的程序员创业有戏
查看>>
[翻译]ADO.NET Entity Framework Beta2(八)/快速入门(实体框架)(3)/生成学校实体数据模型...
查看>>
单机最大tcp连接数
查看>>
跑Java -jar somefile.jar时会发生什么(一个)
查看>>
(转)9款最具代表性的 jQuery 幻灯片效果
查看>>
图的遍历、拓扑排序、最短路径算法
查看>>
【强烈推荐】 超漂亮的仿腾讯弹出层效果(兼容主流浏览器<转>;
查看>>