創造所以我剛剛創建使用sequelize.js節點數據庫,並有這樣的代碼:sequelize JS:數據庫沒有被正確
var sequelize = new Sequelize('database', 'sheta', 'mysqlroot', {
host: 'localhost',
port: 3000,
timestamps: false
});
var User = sequelize.define('User', {
userID: Sequelize.INTEGER,
userName: Sequelize.TEXT,
password: Sequelize.TEXT
}, {
instanceMethods: {
getUserId: function() {
return this.userID;
}
}
});
var Post = sequelize.define('Post', {
userID: Sequelize.INTEGER,
postText: Sequelize.TEXT,
date: Sequelize.DATE,
postID: Sequelize.INTEGER
}, {
instanceMethods: {
getPost: function() {
return this.postText;
}
}
});
var Comment = sequelize.define('Comment', {
postID: Sequelize.INTEGER,
commentText: Sequelize.TEXT,
date: Sequelize.DATE,
commentID: Sequelize.INTEGER
} , {
instanceMethods: {
getComment: function() {
return this.commentText;
}
}
});
var Tag = sequelize.define('Tag', {
tagID: Sequelize.INTEGER,
tagText: Sequelize.TEXT,
postID: Sequelize.INTEGER
});
//Associations
Post.belongsTo(User);
Post.hasOne(User);
User.hasMany(Post);
Post.hasMany(Comment);
Comment.belongsTo(User);
Comment.hasOne(User);
User.hasMany(Comment);
Post.hasMany(Tag);
Tag.hasMany(Post);
sequelize.sync();
但是當我嘗試使用添加一個元組:
User.create({ userID: 1, userName: 'sam', password: 'th'}).success(function(task) {
console.log("inserted");
// you can now access the newly created task via the variable task
});
「插入」從不打印。 我做錯了什麼?我主要看了關於續集js的代碼。有什麼我在這裏失蹤?
您必須確保已創建該表。我建議你創建一個腳本來初始化你的數據庫/表。不要在應用程序代碼中混用它。 – Kiddo 2013-07-29 09:21:51