我正在使用 node-orm 尝试设置我的数据库。这是模型代码。
db = orm.connect("mysql", client, (success, db) ->
Strain = db.define("strain",
name:
type: "string"
validations: [ orm.validators.unique() ]
old_body:
type: "string"
body:
type: "string"
created_at:
type: "date"
update_at:
type: "date"
)
Strain.sync()
)
我将/orm/lib/databases/mysql.js 文件更改为 console.log 返回的同步信息。
this._client.query(_query, function (err, info) {
console.log(err);
console.log(info);
console.log("collection synced");
});
第一次运行 Strain.sync() 时,这是输出。
null
{ affectedRows: 0,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '',
setMaxListeners: [Function],
emit: [Function],
addListener: [Function],
on: [Function],
once: [Function],
removeListener: [Function],
removeAllListeners: [Function],
listeners: [Function] }
collection synced
现在表已按其应有的样子创建了。当我重新启动服务器并再次运行 Strain.sync() 时,这是输出:
null
{ affectedRows: 0,
insertId: 0,
serverStatus: 2,
warningCount: 1,
message: '',
setMaxListeners: [Function],
emit: [Function],
addListener: [Function],
on: [Function],
once: [Function],
removeListener: [Function],
removeAllListeners: [Function],
listeners: [Function] }
collection synced
警告计数跳至“1”,但 err 为 null,且 message 为空。
我需要弄清楚如何更改模型并添加新属性(例如“deleted_at”),并更新表而不丢失数据。我知道我可以执行 Strain.sync(force: true ),但这会删除表然后重新创建它。我只是想更新表,类似于 DataMapper 的 auto_upgrade 函数。
有没有办法用node-orm或者任何与nodejs一起工作的ORM来做到这一点?
最佳答案
很确定当前版本的sync()只创建表,不更新。
Note: sync() only creates tables at the moment; table modifications will be added in an upcoming version.
https://github.com/dresende/node-orm#creating-the-model-on-the-database
关于node.js - node-orm 同步到 Alter Tables(类似于 DataMapper.auto_upgrade),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10728798/