lundi 1 janvier 2018

storedAs column modifier not working in laravel 5.5 migration

I'm trying to add a virtual (persistent) column to my table in Laravel 5.5 using a regular migration:

public function up() {
    Schema::create('works', function (Blueprint $table) {
        $table->increments('id');
        $table->string('cat_name');
        $table->string('cat_prefix');
        $table->unsignedSmallInteger('cat_number');
        $table->string('cat_suffix');
        $table->string('catalog')->StoredAs(
            "concat(cat_name, ' ', cat_prefix, cat_number, cat_suffix)");
        $table->string('title');
    });
}

I have setup logging for all database queries in the project, so I can see the queries performed for php artisan migrate:fresh. The relevant query for the works table is logged as (formatting is mine):

create table `works` (
    `id` int unsigned not null auto_increment primary key,
    `cat_name` varchar(191) not null,
    `cat_prefix` varchar(191) not null,
    `cat_number` smallint unsigned not null,
    `cat_suffix` varchar(191) not null,
    `catalog` varchar(191) not null,
    `title` varchar(191) not null
) default character set utf8mb4 collate utf8mb4_unicode_ci

And indeed, looking at the description of the table in the database:

MariaDB [evh_site]> desc works;
+------------+----------------------+------+-----+---------+----------------+
| Field      | Type                 | Null | Key | Default | Extra          |
+------------+----------------------+------+-----+---------+----------------+
| id         | int(10) unsigned     | NO   | PRI | NULL    | auto_increment |
| cat_name   | varchar(191)         | NO   |     | NULL    |                |
| cat_prefix | varchar(191)         | NO   |     | NULL    |                |
| cat_number | smallint(5) unsigned | NO   |     | NULL    |                |
| cat_suffix | varchar(191)         | NO   |     | NULL    |                |
| catalog    | varchar(191)         | NO   |     | NULL    |                |
| title      | varchar(191)         | NO   |     | NULL    |                |
+------------+----------------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)

Manually creating a similar table in the database (MariaDB 10.0.31) with the right create syntax does work:

create table `works2` (
    ...
    `catalog` varchar(191)
        as (concat(cat_name, ' ', cat_prefix, cat_number, cat_suffix))
        persistent,
    ...

It results in:

MariaDB [evh_site]> desc works2;
+------------+----------------------+------+-----+---------+----------------+
| Field      | Type                 | Null | Key | Default | Extra          |
+------------+----------------------+------+-----+---------+----------------+
| id         | int(10) unsigned     | NO   | PRI | NULL    | auto_increment |
| cat_name   | varchar(191)         | NO   |     | NULL    |                |
| cat_prefix | varchar(191)         | NO   |     | NULL    |                |
| cat_number | smallint(5) unsigned | NO   |     | NULL    |                |
| cat_suffix | varchar(191)         | NO   |     | NULL    |                |
| catalog    | varchar(191)         | YES  |     | NULL    | PERSISTENT     |
| title      | varchar(191)         | NO   |     | NULL    |                |
+------------+----------------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)

The storedAs-method does seem to be the right way as suggested in this Answer.

What am I doing wrong?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ClYsfN
via IFTTT

Aucun commentaire:

Enregistrer un commentaire