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.

Serializing one-to-one relationships

See original GitHub issue

I’m trying to serialize a one-to-one relationship and I can’t figure out to to setup my models and serializers to make this work.

// models/user.js
import { Model } from 'ember-cli-mirage';

export default Model.extend({
  // Should some relationship go here?
})
// models/address.js
import { Model, belongsTo } from 'ember-cli-mirage';

export default Model.extend({
  user: belongsTo()
});
// serializers/user.js
import ApplicationSerializer from './application';

export default ApplicationSerializer.extend({
  relationships: ['address']
});

With this setup, we have address.user and address.userId, but there is no user.address. When serializing user, the address isn’t included in the payload. I was expecting a hasOne() model relationship since we’re dealing with foreign keys in a database, but I see this doesn’t exist.

This may be a conceptual misunderstanding so maybe something could be added to the models documentation to clarify how this should work.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
samselikoffcommented, Oct 26, 2016

I would say to probably use hasMany on this side of the relationship, and clean up the data in Serializer#serialize.

I am actually working through the ORM updates right now, and I’m about halfway through. I have one-to-one and reflexive relationships working at the moment. I’d estimate a few more weeks before this all makes it into master.

2reactions
samselikoffcommented, Jan 23, 2017

This is now supported in the 0-3 series! Read this post for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to serialize a relation OneToOne in Django with Rest ...
You can create custom serializer fields to create your custom PersonSerializer. You can add fields to get values from Enviroment .
Read more >
Nested Relationships in Serializers for OneToOne fields in ...
The solution was to use DRF's Nested Relationships in serialization. I shall assume you have a fair working knowledge of Python, virtualenv, pip ......
Read more >
Serializer relations - Django REST framework
Relational fields are used to represent model relationships. They can be applied to ForeignKey , ManyToManyField and OneToOneField relationships, ...
Read more >
Django Rest Framework API #19 / One To One Relationship ...
Django Rest Framework API #19,You Will Learn:1- How to link two models using One To One relationship.2- How get and show the nested...
Read more >
Serializer Relations - Django REST Framework - GeeksforGeeks
It has an employee field that holds a many-to-one relationship with the Employee model.
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found