title: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Yii2
body: <p>I knew there are a lot of questions the same, but I couldn't find useful information. The answers in other questions is saying that the value doesn't exist in another table, but I didn't understand it. In my case I guess all the values are correct:</p>
  
  <p>This is my <code>item</code> database:</p>
  
  <pre><code>CREATE TABLE `item` (
  `id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `price` double NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  
   --
   -- Indexes for dumped tables
   --
  
    --
    -- Indexes for table `item`
    --
  ALTER TABLE `item`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk-sale-category_id` (`category_id`);
  
  --
  -- AUTO_INCREMENT for dumped tables
  --
  
  --
  -- AUTO_INCREMENT for table `item`
  --
  ALTER TABLE `item`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
  --
  -- Apribojimai eksportuotom lentelėm
  --
  
  --
  -- Apribojimai lentelei `item`
  --
  ALTER TABLE `item`
  ADD CONSTRAINT `fk-sale-category_id` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`);
  
  /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  </code></pre>
  
  <p>And this is my <code>category</code> database:</p>
  
  <pre><code>CREATE TABLE `category` (
  `id` int(11) NOT NULL,
  `name` varchar(80) COLLATE utf8_unicode_ci NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  
  --
  -- Indexes for dumped tables
  --
  
  --
  -- Indexes for table `category`
  --
  ALTER TABLE `category`
  ADD PRIMARY KEY (`id`);
  
  --
  -- AUTO_INCREMENT for dumped tables
  --
  
  --
  -- AUTO_INCREMENT for table `category`
  --
  ALTER TABLE `category`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15;
  </code></pre>
  
  <p>My Yii2 <code>SiteController</code> action:</p>
  
  <pre><code>    public function actionCreate()
      {
      $category = new Category();
      $category-&gt;scenario = Category::SCENARIO_CREATE;
      $item = new Item();
  
  
      if ($category-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $item-&gt;load(Yii::$app-&gt;request-&gt;post())) {
          if ($category-&gt;validate(false) &amp;&amp; $item-&gt;validate(false)) {
                     $item-&gt;category_id = $category-&gt;id;
          return $this-&gt;redirect(['create']);
      }
  }
      return $this-&gt;render('create', [
          'category' =&gt; $category,
          'item' =&gt; $item,
          ]);
      } 
  }
  </code></pre>
  
  <p>In <code>item</code> database there is <code>category_id</code>, which has a foreign key, but that value is in <code>category</code> also, it's `id'. I hope you understand it, thanks for any help</p>