1

Creating a bike rent application and implementing addBike() to database. addCustomer() worked with this code, but I think it is because it has no foreign keys. How do I Insert Into database when half the table is foreign keys?

Thank you!

newbike.js:

add() {
    bikeService.addBike(
      this.wheelsize,
      this.frame,
      this.gears,
      this.typeId,
      this.location,
      this.orderId,
      id => {
        history.push('/bike/' + id);
        this.props.history.push('/regbike');
      }
    );
  }
}

services.js:

    connection.query(
      'insert into Bike (wheelsize, frame, gears, typeId, statusId, location, orderId) values (?, ?, ?, ?, ?, ?, ?)',
      [wheelsize, frame, gears, typeId, statusId, location, orderId],
      (error, results) => {
        if (error) return console.error(error);

        success(results.insertId);
      }
    );
  }
export let bikeService = new BikeService();
Bill
  • 33
  • 4

1 Answers1

0

You should disable constraint check before insert and reenable it afterwards (If you want to insert loads of data)

SET FOREIGN_KEY_CHECKS=0;

and then when you're done

SET FOREIGN_KEY_CHECKS=1;

or just INSERT IGNORE

"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"

It all depends on what you want to achieve

So it would look like this:

  1. Disable the constraint checks.

  2. Insert all the data you have to all your related tables

  3. Enable the constraint checks.

    a) verify that you didn't loose any data.

    b) verify that the constraints are actually enabled

leawp
  • 166
  • 9