zoukankan      html  css  js  c++  java
  • Sequelize-nodejs-13-Working with legacy tables

    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:1
    Organization.belongsTo(User, {foreignKey: 'owner_id'});
    User.hasOne(Organization, {foreignKey: 'owner_id'});
    
    // 1:M
    Project.hasMany(Task, {foreignKey: 'tasks_pk'});
    Task.belongsTo(Project, {foreignKey: 'tasks_pk'});
    
    // N:M
    User.hasMany(Role, {through: 'user_has_roles', foreignKey: 'user_role_user_id'});
    Role.hasMany(User, {through: 'user_has_roles', foreignKey: 'roles_identifier'});
     
  • 相关阅读:
    Java重温学习笔记,Java8新特性:函数式接口
    Java重温学习笔记,Java8新特性:Lambda 表达式
    236. 二叉树的最近公共祖先
    230. 二叉搜索树中第K小的元素
    117. 填充每个节点的下一个右侧节点指针 II
    116. 填充每个节点的下一个右侧节点指针
    111. 二叉树的最小深度
    109. 有序链表转换二叉搜索树
    剑指 Offer 68
    1367. 二叉树中的列表
  • 原文地址:https://www.cnblogs.com/wanghui-garcia/p/10071427.html
Copyright © 2011-2022 走看看