Skip to content

hasMany polymorphic example doesn't not work #446

Closed

Description

@nvdnkpr

Using the example from here: http://docs.strongloop.com/display/LB/Polymorphic+relations#Polymorphicrelations-HasAndBelongsToMany

In all the following cases I tried the following:
I created an author (POST /authors) and then I tried to create this authors' pictures (POST /authors/{ID}/pictures)

The shown example (case 1) gives me this error:
This gives me an TypeError: Cannot read property 'modelName' of null at Function.Model.belongsToRemoting ([…]/node_modules/loopback/lib/models/model.js:346:56).

I figured the following out:

  1. As I wrote: Using Picture.hasMany(…, invert: true) and [Author | Reader].hasAndBelongsToMany(…) creates the right structure for the (through model) PictureLink (id, imageableType, imageableId, pictureId) but fails because of the error shown above. The error occurs because the belongsTo relation is broken.

  2. Using Picture.hasAndBelongsToMany(…, invert:true ) and [Author | Reader].hasAndBelongsToMany(…) creates NOT the right structure for (id, imageableType, imageableId, pictureId, authorid , readerid) and it fails with the same error.

  3. Using Picture.hasMany(…, invert:true ) and [Author | Reader].hasMany(…) creates NOT the right structure for (id) and it fails with an SQL Error because it tries the following query loopback:connector:postgresql SQL: INSERT INTO "public"."checkuupicturelink" () SELECT RETURNING "id"

Activity

  1. fabien commented on Aug 7, 2014

    @fabien
    Contributor

    @nvdnkpr thanks for reporting this, it is indeed a bug. Since the Picture can belong to either Author or Reader, relation.modelTo is not defined as a fixed value; it is dynamically deduced at runtime. The remoting setup that happens during Model.belongsToRemoting tries to access this though, and that is why it fails.

    Could you please check if the fix at https://github.com/fabien/loopback/tree/fix/polymorphic-belongs-to solves this?

  2. nvdnkpr commented on Aug 7, 2014

    @nvdnkpr
    Author

    I tried your fix and it worked. No error at all. Using the (1) setup from above (which is the one from the example).

    I tried it with the postgres connector and also with my own connector for arangoDB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions