【问题标题】:Can't add new attributes to existing model sails.js无法向现有模型sails.js 添加新属性
【发布时间】:2016-01-12 20:32:13
【问题描述】:

我是sails.js 的新手。我需要在sails js中向现有模型添加字段。 这里是

module.exports = {
    attributes: {
        id: {
            columnName: 'id',
            type: 'integer',
            autoIncrement: true,
            primaryKey: true,
            unique: true
        },
        username: {
            columnName: 'username',
            type: 'STRING',
            required: true,
        },
        userLevel: {
            columnName: 'user_level',
            type: 'integer',
            required: true,
            defaultsTo: 1
        },
...

但只要我在我的模型 js 中添加字段,例如

   newAttribute: {
           columnName: 'new_attribute',
           type: 'integer',
           required: true
    }

我收到错误“E_UKNOWN”。 更准确地说,当我尝试使用更新的模型登录时,我收到以下消息:

rr {"data":{"err":{"error":"E_UNKNOWN","status":500,"summary":"Encountered an unexpected error","raw":{"code":"ER_BAD_FIELD_ERROR","errno":1054,"sqlState":"42S22","index":0}}},"status":401,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"data":{*email and password here*},"url":"auth/login","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Unauthorized"}

感谢您的帮助。

UPD 如果我将 NODE_ENV 设置为开发,它就可以工作。可能是connection.js文件的问题吗?

【问题讨论】:

  • 我怀疑既然你说你的新字段是必需的,而你现有的数据没有它,它会抛出一个错误。您是否尝试过为该字段提供defaultsTo
  • 是的,我试过了。不工作
  • 该死。如果你在开发环境中,有没有试过在config/models.js中设置migrate: 'alter'?这样,帆将尝试为您修复它。仍然是一个实验性功能。
  • 是的,我什至尝试使用'drop',但同样的错误不断发生
  • @Tholle 似乎如此。当我手动将 NODE_ENV 设置为开发然后再设置为生产时,它工作正常

标签: javascript node.js sails.js waterline


【解决方案1】:

似乎我找到了解决问题的方法。起初我将 NODE_ENV 设置为开发,并且,我猜,表已创建。然后我将 NODE_ENV 更改为生产。现在它可以正常工作,没有出现奇怪的错误。

【讨论】:

    猜你喜欢
    • 2014-09-27
    • 2015-07-27
    • 1970-01-01
    • 2021-10-18
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    相关资源
    最近更新 更多