coresmart / persistencejs

persistence.js is an asynchronous Javascript database mapper library. You can use it in the browser, as well on the server (and you can share data models between them).
http://persistencejs.org
1.73k stars 240 forks source link

add warning when adding 'id' as property #117

Open ghost opened 11 years ago

ghost commented 11 years ago

I had trouble adding entities with one-to-many relationships. The statement always failed with the following error message:

Error: Property 'container' of 'Item' with id: [object Object] not fetched, either prefetch it or fetch it manually.
[Break On This Error]   

exports.config = persistence.store.memory.config;

using the following program:

var Container = persistence.define( 'Container', { id: 'INT', size: 'INT' } )
var Item      = persistence.define( 'Item', { name : "TEXT" } )
Container.hasMany( 'items', Item, 'container' )

persistence.store.memory.config( persistence )

persistence.schemaSync()

var container = new Container( { size : 10 } )
var item      = new Item     ( { name : 'foo'} )

container.items.add( item )
persistence.add( container )
persistence.flush()

Container.all().list( function( ctns )
{
    console.log( ctns )
})

Then I found that I was actually overwriting a property that persistence is making use of. As 'id' is a common property in relational models, I think persistence should throw a exception when trying to add a property with that name.