简体   繁体   English

Sails.js中的数据库架构

[英]Database schema in Sails.js

How can I add attributes like null , default or other database attributes directly from a model in Sails.js. 如何直接从Sails.js中的模型添加诸如nulldefault或其他数据库属性之类的属性。 I tried adding foreign key too, but it doesn't even affect the structure of my table. 我也尝试添加外键,但是它甚至不影响表的结构。

module.exports = {
  tableName: 'users',
  attributes: {
    name: {
      type: 'string',
      required: true,
      null: true
    },
    email: {
      type: 'string',
      required: true,
      unique: true
    },
    password: {
      type: 'string',
      required: true
    },
    point: {
      type: 'integer',
      defaultsTo: 0
    }
  }
};

I think you are missing the point of ORM here. 我认为您在这里缺少ORM的要点。 The thing is, sails.js doesn't "know" what type of database you are using. 问题是,sails.js不会“知道”您正在使用哪种数据库。 Is it a relational one, like mySQL, or is it a MongoDB, or maybe Redis. 它是一种关系数据库,例如mySQL,还是MongoDB,或者Redis。 So, for example, the default value is set by sails in time of creating a record (if a value for that attribute is not provided), and not by some SQL statement, because the DB might not be SQL based. 因此,例如,默认值是由创建记录时的帆设置的(如果未提供该属性的值),而不是由某些SQL语句设置的,因为数据库可能不是基于SQL的。 So there are limitations in Waterline, which is the default ORM of sails. 因此,“水线”中存在一些限制,它是风帆的默认ORM。 If you want to use sails in a different way, you can replace Waterline or use a direct database adapter. 如果要以其他方式使用风帆,则可以替换“水线”或使用直接数据库适配器。 But how can you do that is beyond the scope of this question. 但是您如何做到这一点超出了这个问题的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM