Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save a object with hasMany/hasOne relations ? #2104

Open
cedmallet opened this issue Feb 12, 2021 · 0 comments
Open

How to save a object with hasMany/hasOne relations ? #2104

cedmallet opened this issue Feb 12, 2021 · 0 comments

Comments

@cedmallet
Copy link

cedmallet commented Feb 12, 2021

I'm looking for the nicest/recommanded way to save an object and its relations at the same time, and within a transaction.

Let's say I have Flight Model having 0..N relations Instructions.

Flight = bookshelf.model('Flight', {
	tableName: 'flight',
	idAttribute: 'flightId',

	instructions() {
		return this.hasMany(Instruction, 'flightId', 'flightId');
	}
 }

Now i'm building a Model with all fields :

let flight = new Flight({... });
let instruction = new Instruction({...});

and I just want to save them all together.

Do I have to do it all manually, ie :

  • save the Flight
  • set the flightId in the instruction
  • save the instruction
    all of it with a transaction being cast from call to call ?

or can I just set the value of the relation with
flight.set('instructions', [instruction]) ?
flight.instructions().attach(instruction) ?
and save it all ?

It seems quite confusing that basic relationship handling doesn't seem to be covered, is that's the case.

Any hint highly appreciated,
many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant