Relationship between same entity

139 views
Skip to first unread message

Aristeidis Bampakos

unread,
Apr 24, 2014, 5:56:49 PM4/24/14
to persis...@googlegroups.com
Hello,

Is it possible to have relationship between records of the same entity? For example:


var Attraction = persistence.define('attraction', {
          name: "TEXT",
          description: "TEXT"
});

Attraction.hasMany('relAttractions', Attraction, 'attraction')


That is, an attraction can have many attractions as relAttractions.

Thanks,

Zef Hemel

unread,
Apr 25, 2014, 3:28:57 AM4/25/14
to persis...@googlegroups.com
I think that should work, yes as long as you have different property names for each end (as you do).

-- Zef


--
You received this message because you are subscribed to the Google Groups "persistence.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to persistencej...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aristeidis Bampakos

unread,
Apr 25, 2014, 6:20:08 AM4/25/14
to persis...@googlegroups.com
Hello Zef,

Thank you very much for your response.
I have created a method that adds an attraction with its related ones and seems to be working fine, since I can see data correctly through my app. When I refresh the page, I get the following error in the call of persistence.loadFromLocalStorage() :

Error: Property 'attraction' of 'attraction' with id: '9D2A124C-6CAE-4AC6-8815-F21E3060E553' not fetched, either prefetch it or fetch it manually.
If I use a separate entity for related attractions, the error does not show up.

Zef Hemel

unread,
Apr 25, 2014, 9:49:55 AM4/25/14
to persis...@googlegroups.com
How are you loading the data on page load?

-- Zef

Aristeidis Bampakos

unread,
Apr 25, 2014, 9:53:49 AM4/25/14
to persis...@googlegroups.com
persistence.loadFromLocalStorage();
// find the attraction by ID Attraction.load(id, function(a){ if(a != null){ callback(a); } });

Zef Hemel

unread,
Apr 25, 2014, 9:58:41 AM4/25/14
to persis...@googlegroups.com
Alright, the problem may be that you have to fetch the "attraction" property explicitly before you access it using the "fetch" method:


Attraction.load(id, function(a){
  if(a != null
){
       a.fetch("attraction", function() {
          callback(a);
       });
  }
});
When you use Attract.list() queries, you can do this by using .prefetch("attraction") (which will generate a JOIN for you), also see the documentation on "prefetch" and "fetch" in the README.md

-- Zef

Aristeidis Bampakos

unread,
Apr 25, 2014, 10:20:19 AM4/25/14
to persis...@googlegroups.com
OK, but why is the error caught in the first line of the code:

persistence.loadFromLocalStorage();

and not afterwards?

Zef Hemel

unread,
Apr 25, 2014, 10:32:19 AM4/25/14
to persis...@googlegroups.com
It's only thrown if the object in question (the one that the .attraction attribute points to) is not already loaded in memory. In the other context probably you had the object in memory already so it works, but on a fresh page load that won't be the case.

-- Zef

Aristeidis Bampakos

unread,
Apr 25, 2014, 11:54:37 AM4/25/14
to persis...@googlegroups.com
When persistence.loadFromLocalStorage() is called, does't load the database in memory? Should I call some other function to do the trick?

Aristeidis Bampakos

unread,
Apr 25, 2014, 1:16:17 PM4/25/14
to persis...@googlegroups.com
I made some debugging in persistence.store.memory.js and found that the error is thrown in method persistence.set (line 93) of persistence.js when tries to set attraction property. 
Perhaps, it falls in some sort of cyclical reference because the attraction property of the current Attraction has its own attraction property which has another attraction property etc....
Reply all
Reply to author
Forward
0 new messages