dresende/node-orm — Issue #24: People.find doesn't return corrent person_id value
Author: abcd-ca
Created: 2011-12-31 05:05:09+00:00
State: open

This is coffeescript but you'll get the idea. I can get People.find to return the correct value if I do it right after doing setParent but that's cause it's in memory. If I quit my app and restart the app then run this, the value of parent_id is 0 even though using my MySQL client shows me that the value is in fact 2. Therefore, the subsequent call to john.getParent fails. Again, john.getParent doesn't fail if run immediately after setParent because it's in memory still. I'm coding for the use case where the app has crashed and something like Forever has restarted it so values need to be recovered from the database. I cannot rely on memory.

assert = require 'assert'
orm = require("orm");
#establish the db connection
db = orm.connect "mysql://u:p@1.2.3.4/mydb", (success, db) ->
if !success
console.log "Could not connect to database!"
else
console.log "success"

# you can now use db variable to define models

People = db.define "people"
name: { "type": "string" },
surname: "string", #@ shortcut
age: Number # another shortcut

People.hasOne("parent");

People.sync()

People.find
name:'john'
,
1
,
(results) =>
console.log "john results:"
console.dir results[0]

#result is that person_id is 0 regardless of fact that actual value in db is (in my case) 2
return