osroflo's avatar

Migration comment not working in Postgres SQL

Lumen: 5.4 Postgres: 9.3.10 PHP: 5.6.29

This is my migration code:

public function up()
{
    Schema::create('user_permission', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('client_id')->comment('Foreign key to relate with client.');
        $table->integer('scope_id')->comment('Foreign key to relate with application scope.');
        $table->integer('entity_id')->nullable();
        $table->integer('permission_id')->comment('Foreign key to relate with application permissions.');
        $table->timestamps();
        
        // foreign keys
        $table->foreign('oauth_client_id')->references('id')->on('client');
        $table->foreign('scope_id')->references('id')->on('scope');
        $table->foreign('permission_id')->references('id')->on('permission');
    });
}

If I migrate to a MySQL db, I can see that the comment sql is being added:

php artisan migrate --pretend

Migration table created successfully.


CreateUserPermissionTable: 
create table `user_permission` (
    `id` int unsigned not null auto_increment primary key, 
    `client_id` int not null comment 'Foreign key to relate with client.', 
    `scope_id` int not null comment 'Foreign key to relate with application scope.', 
    `entity_id` int null, 
    `permission_id` int not null comment 'Foreign key to relate with application permissions.', 
    `created_at` timestamp null, 
    `updated_at` timestamp null
) 

default character set utf8mb4 collate utf8mb4_unicode_ci

But if I migrate to a Postgres db. The comment sql is not added:

php artisan migrate --pretend

CreateUserPermissionTable: create table "user_permission" (
    "id" serial primary key not null,
    "client_id" integer not null,
    "scope_id" integer not null,
    "entity_id" integer null,
    "permission_id" integer not null,
    "created_at" timestamp(0) without time zone null,
    "updated_at" timestamp(0) without time zone null
)

After I migrate to a postgres db the comment is never added. Any help will be really appreciated.

Thanks

0 likes
1 reply
osroflo's avatar

I share this, in case it is useful for someone else:

<?php
class AddTaskTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        DB::connection()->getPdo()->exec(
            "CREATE TABLE task (
                id SERIAL PRIMARY KEY,
                done boolean,
                title varchar(100),
                description text,
                created_at timestamp without time zone DEFAULT NOW(),
                updated_at timestamp without time zone DEFAULT NOW()
            );

            COMMENT ON TABLE task IS 'This table stores tasks';
            COMMENT ON COLUMN task.done IS 'If the task was done';
            COMMENT ON COLUMN task.title IS 'The task title';
            COMMENT ON COLUMN task.description IS 'The task description';"
        );
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::connection()->getPdo()->exec('DROP TABLE task');
    }
}

Please or to participate in this conversation.