1

所以我刚刚使用 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
});

“插入”从不打印。难道我做错了什么?主要看sequelize js上的代码。我在这里缺少什么吗?

4

1 回答 1

3

关于您的问题可能是什么的一些建议。

您说您使用 sequelize 创建了数据库 - 您必须已经创建了一个数据库,sequelize 只创建表 - 只是检查;-)

您能否将.erroror.done函数附加到sequelize.syncandUser.create调用并发布出现的错误

于 2013-03-12T12:25:09.223 回答