Skip to content

Setting up relations upon creation #17

Closed
marcelklehr opened this Issue Feb 28, 2014 · 8 comments

2 participants

@marcelklehr

So, I've got a model Apple:

var Apple = Backbone.Model.extend({
  schema: {
    origin: ['belongsTo', Farm]
  }
})

And a Juice model:

var Juice= Backbone.Model.extend({
  schema: {
    origin: ['belongsTo', Farm]
  }
})

Question: Can I simply do the following to overtake the apple's origin?

var juice = new Juice({
  origin: juicyApple.get('origin')
})
Juice.save(...)
@kmalakoff
Vidigami member

It'll depend on the reverse relationship. If it is hasMany, not automatically. If it is hasOne, yes.

By default, if unspecified, we set them to hasMany so you'll need to add an explicit reverse relationship of type hasOne.

@marcelklehr

uh? Why does it depend on the reverse relationship? BelongsTo stores the foreignKey on "this" model, right? both Apple and Juice have a simple ['belongsTo', Farm] relationship..

How could I get this done otherwise?

@kmalakoff
Vidigami member

Because belongsTo could belong to one thing (hasOne) or multiple things (hasMany)...just following the convention of other ORMs.

@marcelklehr

as far as I know, belongsTo means that the entity in question belongs to one other entity, that other entity could have others that belong to it or just one, but that shouldn't matter to the first one, i think. does that make sense?

For the sake of this example let's say the imaginary Farm hasMany apples and many juices.

So, how would I go about making apple juice out of my apple (overtake the apple's origin)?

@kmalakoff
Vidigami member

The relationship between belongsTo and the reverse relationship defines where the foreign key is stored: on the belongTo model (hasOne) or in a join table (hasMany).

Another attempt....if I understand correctly, you would probably have to write it like:

var Apple = Backbone.Model.extend({
  schema: {
    origin: ['belongsTo', Farm]
  }
})
var Juice= Backbone.Model.extend({
  schema: {
    origin: ['belongsTo', Farm]
  }
})

var Farm= Backbone.Model.extend({
  schema: {
    apples: ['hasMany', Apple, foreign_key: 'origin_id'],
    juices: ['hasMany', Juice, foreign_key: 'origin_id']
  }
})

The apple would belong to both simultaneously since setting the origin on create wouldn't be updating a hasOne relationship, but maintaining two different hasMany relationships.

Did I get what you meant this time?

@marcelklehr

Slightly off topic: Why do you need a separate Join table, if you have a 1:n relationship?

Your code shows the model I had in mind; my question is now, how can I set up a new Apple/Juice?
Can I simply do:

function makeJuice(myJuicyApple) {
  var juice = new Juice({
    origin: myJuicyApple.get('origin') // <-- will this work?
  })
  return juice
}
@kmalakoff
Vidigami member

As for join tables...for 1:n, the foreign key is stored on the belongsTo; whereas, n:m use join tables. We just do what most other solutions do. Just look at another ORM like ActiveRecord and the configuration and underlying database schema should be clear.

I'm really not sure why this issue isn't resolved yet. If your code doesn't work as expected, please write a test, try to get it to work, and submit a pull request to patch it. If it is a learning curve thing, I think I've explained enough for you to solve your problem. Good luck!

@kmalakoff kmalakoff closed this Mar 2, 2014
@marcelklehr

I guess I was just a little confused, thanks for your patience :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.