Ответы пользователя по тегу Миграция базы данных
  • Как привязать поля таблиц в миграциях?

    @masterfreelance
    программист со стажем
    У вас поля "{{%phone}}.company" и "{{%company}}.name" не индексные, поэтому внешняя связь и не создается.
    И зачем вы делаете связь по полям "{{%phone}}.company" и "{{%company}}.name"? У вас же есть primaryKey в каждой таблице.
    Я бы сделал так:
    public function up()
    {
      $tableOptions = null;
      if ($this->db->driverName === 'mysql') {
        $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
      }
      // Организации
      $this->createTable('{{%company}}', [
        'id' => $this->primaryKey(),
        'name' => $this->string(),
        'code' => $this->string(),
        'city' => $this->string(),
        'address' => $this->string(),
        'schedule' => $this->string(),
        'link' => $this->string(),
      ], $tableOptions);
    
      // Телефоны
      $this->createTable('{{%phone}}', [
        'id' => $this->primaryKey(),
        'company_id' => $this->integer(),
        'number' => $this->string(),
      ], $tableOptions);
      $this->createIndex('idx-phone-company_id', '{{%phone}}', 'company_id');
      $this->addForeignKey("fk-phone-company", "{{%phone}}", "company_id", "{{%company}}", "id", 'SET NULL', 'RESTRICT');
    }
    Ответ написан
    Комментировать