Several errors on associations with graph-like models #27

Closed
helllamer opened this Issue Aug 1, 2012 · 3 comments

Projects

None yet

3 participants

@helllamer
Contributor

Let's create two simple models. One model will store users:

-module(person, [Id, Name]).
-compile(export_all).

%% User-to-user relations stored in second model (person_relation)
-has({relations_in, many, [
        {module,        person_relation},
        {foreign_key,   to_person_id},
        {order_by,      from_person_id},
        {descending,    true}
    ]}).

-has({relations_out, many, [
        {module,        person_relation},
        {foreign_key,   from_person_id},
        {order_by,      to_person_id},
        {descending,    true}
    ]}).

Second model is used to store relations between people:

-module(person_relation, [Id, FromPersonId, ToPersonId]).
-compile(export_all).

-belongs_to_person(from_person).
-belongs_to_person(to_person).

Let's try to use these models. Assume, that person model already have person-1 and person-2:

> PR = person_relation:new(id, "person-1", "person-2").
{person_relation, id, "person-1", "person-2"}

> PR:save().
{ok, {person_relation, "person_relation-1", "person-1", "person-2"}}

> boss_db:find(person_relation, []).
[{person_relation, "person_relation-1", "from_person-1", "to_person-2"}].

First bug: boss_db:find returns unexpected values in from_person_id and to_person_id attrs. In other words, these values are useless. Should be "person-1" and "person-2" as I saved earlier.


Lets find something in relations:

> boss_db:find(person_relation, [{from_person_id, equals, "person-1"}]).
{error,{error,error,<<"22P02">>,
              <<"invalid input syntax for integer: \"person-1\"">>,
              [{position,<<"52">>}]}}

> boss_db:find(person_relation, [{to_person_id, equals, "to_person-1"}]).
{error,{error,error,<<"22P02">>,
              <<"invalid input syntax for integer: \"to_person-1\"">>,
              [{position,<<"52">>}]}}

In PostgreSQL logs this strings:

ERROR:  invalid input syntax for integer: "person-1" at character 52
STATEMENT:  SELECT * FROM people_relation WHERE to_person_id = 'person-1' AND  TRUE ORDER BY id ASC

ERROR:  invalid input syntax for integer: "to_person-1" at character 52
STATEMENT:  SELECT * FROM people_relation WHERE to_person_id = 'to_person-1' AND  TRUE ORDER BY id ASC

So, this is a second bug.

Contributor

I've fixed the first bug in my fork in commit DiamondLovesYou@af04966. I haven't issued a pull request because I also fixed the bug for MySQL and haven't tested the fix for that db.

Try without the type addition to the id. Off the top of my head I'm unsure what the docs (in ChicagoBoss, that is) say about filtering by ids in boss_db:find, if anything.

Contributor

My fix also patched your second bug:

> boss_db:find(person_relation, [{from_person_id, 'equals', "person-1"}]).
[{person_relation,"person_relation-1","person-1",
                  "person-2"}]

Originally, the backend adapters failed to account for -belongs_to_() associations; but as far as I could tell, the bug occured only while 'activating' the record after a lookup. I honestly can't say how my commit fixed this; I'm still looking, but so far it looks like my commit didn't touch any code used for the call.

Contributor
zkessin commented Jan 23, 2014

It looks like this was merged, is it working now?

@zkessin zkessin closed this Jan 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment