table('users')->hasColumn('organisation_id'); if (!$exists) { $this->table('users') ->addColumn('organisation_id', 'integer', [ 'default' => null, 'null' => true, 'signed' => false, 'length' => 10 ]) ->addIndex('organisation_id') ->update(); } $q1 = $this->getQueryBuilder(); $org_id = $q1->select(['min(id)'])->from('organisations')->execute()->fetchAll()[0][0]; if (!empty($org_id)) { $q2 = $this->getQueryBuilder(); $q2->update('users') ->set('organisation_id', $org_id) ->where(['organisation_id IS NULL']) ->execute(); } } }