2016-03-07 49 views
1

當設置sails模型以使用帶有字符串主鍵(PK)的現有MySql數據庫時,不再自動生成PK。 我錯過了什麼,或者我是否需要在beforeCreate實施PK一代?如果我要實現PK生成,我不確定如何避免與傳統數據庫中的現有密鑰發生重大沖突。在新的sails應用程序中使用傳統mysql數據庫

調用Cat.create().exec(console.log)引發以下錯誤:

Invalid attributes sent to User: 
• id 
    • `undefined` should be a string (instead of "null", which is a object) 
    • "required" validation rule failed for input: null 

該模型具有下面的代碼:

module.exports = { 
connection: 'laraschMysql', 
schema: true, 
identity: 'Contest', 
tableName: 'Contest', 
migrate: 'safe', 
autoPK: false, 
autoCreatedAt: false, 
autoUpdatedAt: false, 
autoUpdatedAt: false, 
    attributes: { 
    id: { 
     type: 'string',  //varchar(255) in the db                  
     autoIncrement:true, // not working? 
     primaryKey: true, 
     required: true 
    } 
} 

將更加普遍:有哪些常見的問題/使用傳統時絆腳石/問題數據庫與新的sails應用程序?

有沒有什麼好的指南(sails文檔似乎沒有太深入地討論這個主題)來設置一個帶有sails的遺留數據庫?

回答

0

基於Sails.js文檔自動增量必須總是類型: '整數'

autoIncrement

Sets up the attribute as an auto-increment key. When a new record is added to the model, if a value for this attribute is not specified, it will be generated by incrementing the most recent record's value by one. Note: Attributes which specify autoIncrement should always be of type: 'integer'. Also, bear in mind that the level of support varies across different datastores. For instance, MySQL will not allow more than one auto-incrementing column per table.

相關問題