title: Yii2 - cannot add foreign key constraint
body: <p>I am coming from the Laravel world. I know that foreign keys are necessary for relations to work correctly. This is only my second day with the Yii2 framework.</p>
  
  <p>I have set my primary indexes and foreign keys accordingly, as you can see in my code. I have two tables: A country table and a city table. A city can belong to a country, and a country has many cities:</p>
  
  <p><strong>Countries</strong></p>
  
  <pre><code>
  use yii\db\Migration;
  
  /**
   * Handles the creation of table `{{%countries}}`.
   */
  class m190313_154240_create_countries_table extends Migration
  {
      /**
       * {@inheritdoc}
       */
      public function safeUp()
      {
          $this-&gt;createTable('{{%countries}}', [
              'id' =&gt; $this-&gt;primaryKey(),
              'name' =&gt; $this-&gt;string(),
  
          ]);
          // add primary index
          $this-&gt;createIndex(
              '{{%idx-country_id}}',
              '{{%countries}}',
              'id'
          );
      }
  
      /**
       * {@inheritdoc}
       */
      public function safeDown()
      {
          $this-&gt;dropTable('{{%countries}}');
      }
  }
  </code></pre>
  
  <p><strong>Cities</strong></p>
  
  <pre><code>
  use yii\db\Migration;
  
  /**
   * Handles the creation of table `{{%cities}}`.
   * Has foreign keys to the tables:
   *
   * - `{{%country}}`
   */
  class m190313_192616_create_cities_table extends Migration
  {
      /**
       * {@inheritdoc}
       */
      public function safeUp()
      {
          $this-&gt;createTable('{{%cities}}', [
              'id' =&gt; $this-&gt;primaryKey(),
              'country_id' =&gt; $this-&gt;integer()-&gt;notNull(),
              'name' =&gt; $this-&gt;string('100')-&gt;notNull(),
          ]);
  
          // creates index for column `country_id`
          $this-&gt;createIndex(
              '{{%idx-cities-country_id}}',
              '{{%cities}}',
              'country_id'
          );
  
          // add foreign key for table `{{%country}}`
          $this-&gt;addForeignKey(
              '{{%fk-cities-country_id}}',
              '{{%cities}}',
              'country_id',
              '{{%country}}',
              'id',
              'CASCADE'
          );
      }
  
      /**
       * {@inheritdoc}
       */
      public function safeDown()
      {
          // drops foreign key for table `{{%country}}`
          $this-&gt;dropForeignKey(
              '{{%fk-cities-country_id}}',
              '{{%cities}}'
          );
  
          // drops index for column `country_id`
          $this-&gt;dropIndex(
              '{{%idx-cities-country_id}}',
              '{{%cities}}'
          );
  
          $this-&gt;dropTable('{{%cities}}');
      }
  }
  </code></pre>
  
  <p>Yii2 does not appear to be as well documented as Laravel is. Google says I need to add primary keys. Did so, no luck. What am I missing?</p>