question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to create relationship for polymorphic association

See original GitHub issue

for polymorphic association, how to create relationship.

class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true
end
 
class Employee < ApplicationRecord
  has_many :pictures, as: :imageable
end
 
class Product < ApplicationRecord
  has_many :pictures, as: :imageable
end

create_table :pictures do |t|
      t.string  :name
      t.integer :imageable_id
      t.string  :imageable_type
      t.timestamps
end

how to create relationship, so i can query like this

{
     products {
         name
         pictures {
              id
              name
         }
     }
}

thanks !

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
0x777commented, Dec 6, 2018

@dfang @SkaterDad

We don’t natively support polymorphic associations like that of Rails, however you’ll still be able to query like you wanted. The pictures table will be something along these lines:

pictures:
- id: integer 
- name: text 
- imageable_id: integer
- imageable_type: text

You can create views as follows:

create view employee_pictures as select * from pictures where imageable_type = 'employees';
create view product_pictures as select * from pictures where imageable_type = 'products';

Now you can define a manual relationship called pictures on employees and products table using these views. So your queries will now look like this:

{
  employees {
    id
    name
    pictures {
      id
      name
    }
  }
}

Another option is to use through table like @shahidhk suggested, i.e, employee_pictures, product_pictures … etc. If you have such a modelling, Hasura makes it very easy to insert into these tables using relationships, i.e, you can insert a picture and associate it with an employee in a single query. If you have such a modelling, your queries will look like this:

{
  employees {
    id
    name
    pictures {
      picture {
        id
        name
      }
    }
  }
}

So it is up to you to pick a schema design that suits your use case. The GraphQL queries will roughly be the same.

0reactions
iam4xcommented, Dec 27, 2018

Hi @0x777

Thank you for the solution, nice workaround with views 👍

But I do have an issue, after creating my new relationship the UI for data schema crash (JS error) with the message “Something went wrong”

If I remove the relationship directly in the database it works again. I’ve traced the JS error to this line: https://github.com/hasura/graphql-engine/blob/c3f4c35141f1187fc2f0f107158c1dff26f34568/console/src/components/Services/Data/TableRelationships/autoRelations.js#L110

Here is my faulty hdb_relationship row:

table_schema table_name rel_name rel_type rel_def comment is_system_defined
public orders states array {"manual_configuration": {"remote_table": "orders_states", "column_mapping": {"id": "resource_id"}}} NULL f

Did I missed something or this is a bug? Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Ways to Model Polymorphic Associations in Rails 5 - Medium
So, while you can't model a one to many relationship with a reverse relation, it can be a good option for a one...
Read more >
Rails Techniques: Using Polymorphic Associations - Semaphore
In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models.
Read more >
Understanding Polymorphic Relationships - DevDojo
Adding polymorphic relationships can make your app more efficient, flexible, and easier to program. Digging Deeper. Polymorphic relationships ...
Read more >
Modeling Polymorphic Associations in a Relational Database
Another approach is to use a join table for each relationship type. create table acl_document( acl_id integer not null unique ...
Read more >
Polymorphic Associations - Sequelize
Define the junction model explicitly, specifying the two foreign keys as tagId and taggableId (this way it is a junction model for a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found