2013-12-13 33 views
1

我剛開始進入Sails for Node的框架。但是,似乎我無法在將示例用戶添加到sails-mysql數據庫時獲得獨特的需求。我可以使用相同的用戶名和電子郵件添加無限數量的新用戶。Unique對Node.js不起作用Sails.js「sails-mysql」

從我已閱讀它應該工作,我也嘗試與帆內存和那裏確切的代碼確實工作。這是我錯過的東西嗎?

module.exports = { 

attributes: { 

username: { 
    type: 'string', 
    required: true, 
    unique: true 
}, 

firstname: { 
    type: 'string', 
    required: true 
}, 

lastname: { 
    type: 'string', 
    required: true 
}, 

password: { 
    type: 'string', 
    required: true 
}, 

birthdate: { 
    type: 'date', 
    required: true 
}, 

email: { 
    type: 'email', 
    required: true, 
    unique: true 
}, 

phonenumber: 'string', 

// Create users full name automaticly 
fullname: function(){ 
    return this.firstname + ' ' + this.lastname; 
} 

} 


}; 

正如我上面提到的,這可以與內存存儲一起工作。現在我也嘗試用mongodb來做它的翅片。

+0

可能的重複:http://stackoverflow.com/questions/19101531/unique-property-fails-in-sails-js – bredikhin

+0

他說他解決了autoPK:false,但這只是刪除我的id列。他接受了帆的記憶作爲答案,但那對我沒有任何幫助。 –

回答

3

在Twitter上得到了Sails.js的支持:「它使用db層 - 懷疑它是自動化問題,你會嘗試新的MySQL數據庫嗎?」

這個答案沒有工作,一個新的數據庫,一切都只是工作:)

1

只需添加到這一點,因爲帆使用自動遷移,如果您最初啓動服務器,並在模型中沒有屬性唯一的是,表格沒有獨特的(索引)開關。如果您然後將模型中的現有屬性更改爲unique,則在啓動服務器的後續時間內不會重建該表。在開發過程中

一種解決方法是在你的模型設置遷移到drop這樣的:

module.exports = { 

    migrate: 'drop' // drops all your tables and then re-create them Note: You loose underlying. 

    attributes: { 
    ... 
    } 
}; 

這樣一來,該數據庫將在每次啓動服務器時重建。這當然會丟棄任何現有的數據。