title: Yii2 - Integrity constraint violation – yii\db\IntegrityException
body: <p>I have a rather weired error on trying to add data to my database. It keeps telling me that there is an Integrity violation, i.e. </p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ticketing_system`.`parcels`, CONSTRAINT `FK_parcels_customers_customer_id` FOREIGN KEY (`sender_id`) REFERENCES `customers` (`customer_id`))
  The SQL being executed was: INSERT INTO `parcels` (`parcel_id`) VALUES (DEFAULT)
  </code></pre>
  
  <p>I have checked the values being inserted and all of them are picked from the referenced table. Funny bit is that when I echo the values and use them to update the database directly via SQL query on the PHPMyAdmin, the database is populated well without any problem. Again, when I try to change the order of the foreign keys as they appear on the SQL query being performed, the first one fails, meaning all the foreign keys of the model are failing.</p>
  
  <p><strong>EDITED</strong></p>
  
  <p>Here is my actionCreate in the ParcelsController</p>
  
  <pre><code>public function actionCreate()
  {
      $model = new Parcels();
      $customerModel = new Customers();
  
      //checking whether we are getting the logged in user id value
      Yii::info("User id=".Yii::$app-&gt;user-&gt;id); 
  
      $model-&gt;received_by_id = Yii::$app-&gt;user-&gt;id;
  
      if (Yii::$app-&gt;request-&gt;post()) {
  
          $data = Yii::$app-&gt;request-&gt;post('Customers');
  
          $sender_id = Customers::create([
              'name' =&gt; $data['name'],
              'mobile_number' =&gt; $data['mobile_number'],
              'sex' =&gt; $data['sex'],
              'address' =&gt; $data['address'],
              'registered_by_id' =&gt; $model-&gt;received_by_id,
          ]);
  
          $data = Yii::$app-&gt;request-&gt;post('Parcels');
  
          $receiver_id = Customers::create([
              'name' =&gt; $data['name'],
              'mobile_number' =&gt; $data['mobile_number'],
              'sex' =&gt; $data['sex'],
              'address' =&gt; $data['address'],
              'registered_by_id' =&gt; $model-&gt;received_by_id,
          ]);
  
          $model-&gt;consignment_number = $model-&gt;generateUniqueRandomString("consignment_number");
          $model-&gt;source_id = $model-&gt;receivedBy-&gt;station_id;
  
          $model-&gt;parcel_id = Parcels::create([
              'consignment_number' =&gt; $model-&gt;consignment_number,
              'sender_id' =&gt; $sender_id,
              'receiver_id' =&gt; $receiver_id,
              'source_id' =&gt; $model-&gt;source_id,
              'destination_id' =&gt; $data['destination_id'],
              'type_id' =&gt; $data['type_id'],
              'weight' =&gt; $data['weight'],
              'cost' =&gt; $data['cost'],
              'parcel_info' =&gt; $data['parcel_info'],
              'received_by_id' =&gt; $model-&gt;received_by_id,
          ]);
  
          // return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;parcel_id]);
      } else {
          return $this-&gt;render('create', [
              'model' =&gt; $model, 'customerModel' =&gt; $customerModel,
          ]);
      }
  }
  </code></pre>
  
  <p>Customers::create function in the Customers Model</p>
  
  <pre><code>public static function create($data)
  {
  
      $model = new self;
  
      $mobile_number = $data['mobile_number'];
  
      $exists = $model-&gt;find()-&gt;where( [ 'mobile_number' =&gt; $mobile_number ] )-&gt;exists();
  
      if($exists) {
          $existing_customer = Customers::find()
              -&gt;where('mobile_number = :mobile_number', [':mobile_number' =&gt; $mobile_number])
              -&gt;one();
          return $existing_customer['customer_id'];
      }
      else {
          $model-&gt;name = $data['name'];
          $model-&gt;registered_by_id = $data['registered_by_id'];
          $model-&gt;mobile_number = $data['mobile_number'];
          $model-&gt;sex = $data['sex'];
          $model-&gt;address = $data['address'];
          $model-&gt;status = 10;
          $model-&gt;save();
          return $model-&gt;getPrimaryKey();
      }
  
  }
  </code></pre>
  
  <p>Parcels::create function in the Parcels Model</p>
  
  <pre><code>public static function create($data)
  {
      $model = new self;
  
      $consignment_number = $data['consignment_number'];
      $receiver_id = $data['receiver_id'];
      $sender_id = $data['sender_id'];
      $source_id = $data['source_id'];
      $destination_id = $data['destination_id'];
      $type_id = $data['type_id'];
      $weight = $data['weight'];
      $cost = $data['cost'];
      $parcel_info = $data['parcel_info'];
      $received_by_id = $data['received_by_id'];
  
      $model-&gt;save(false);
      return $model-&gt;getPrimaryKey();
  
      // echo var_dump($data);
      // echo "Consignment Number: ".$consignment_number." - Sender: ".$sender_id." - Receiver: ".$receiver_id." - From: ".$source_id." - To: ".$destination_id." - Type: ".$type_id." - Weight: ".$weight." - Cost: ".$cost." - Parcel Info: ".$parcel_info." - Received By: ".$received_by_id;
  }
  </code></pre>
  
  <p>And here is the function generateUniqueRandomString() in the Parcels Model</p>
  
  <pre><code>public function generateUniqueRandomString($attribute, $length = 10) {
  
      $randomString = Yii::$app-&gt;getSecurity()-&gt;generateRandomString($length);
  
      $randomString = strtoupper($randomString);
  
      if(!$this-&gt;findOne([$attribute =&gt; $randomString]))
          return $randomString;
      else
          return $this-&gt;generateUniqueRandomString($attribute, $length);
  
  }
  </code></pre>