Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phinx migrations - explicit length of integer column

Can you tell me please if is possible to set explicit length of integer column via Phinx migrations addColumn() method?

Documentation uses limit option with MysqlAdapter::INT_REGULAR like ['limit' => MysqlAdapter::INT_SMALL, 'signed' => false] but it automatically sets the length for column e.g. int(10).

But what should I do if I need int(11) e.g for foreign key column?

Thx.

like image 486
Čamo Avatar asked Dec 04 '25 13:12

Čamo


1 Answers

As I understood limit option MysqlAdapter::INT_REGULAR is something like predefined types in Phinx. But you can also use your own limit variable.

Here is an example:

// using Phinx 0.5.4
public function change() {
  $table = $this->table('papers');
  $table->addColumn('user_id', 'integer', ['limit' => 2])
        ->addColumn('book_id', 'integer') // by default will be int(11)
        ->addColumn('bank_id', 'integer', ['limit' => 32])
        ->create();

}

MySQL describe results:

+---------+---------+------+-----+---------+----------------+
| Field   | Type    | Null | Key | Default | Extra          |
+---------+---------+------+-----+---------+----------------+
| id      | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id | int(2)  | NO   |     | NULL    |                |
| book_id | int(11) | NO   |     | NULL    |                |
| bank_id | int(32) | NO   |     | NULL    |                |
+---------+---------+------+-----+---------+----------------+

To get more information please check the source code of getSqlType() and source code of getPhinxType() functions.

like image 58
tsg Avatar answered Dec 07 '25 03:12

tsg