Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Create table failed #106

Closed
yassinrais opened this issue Feb 28, 2021 · 4 comments
Closed

[BUG] Create table failed #106

yassinrais opened this issue Feb 28, 2021 · 4 comments
Assignees
Labels
bug Something isn't working PostgreSQL

Comments

@yassinrais
Copy link

Runtime Error: Failed to create table 'subscription'. In 'SubscriptionMigration_1614549884490182' migration. DB error: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "dma.company" does not exist .

@Jeckerson Jeckerson added bug Something isn't working PostgreSQL labels Feb 28, 2021
@Jeckerson Jeckerson added this to the 2.2.x milestone Feb 28, 2021
@Jeckerson Jeckerson self-assigned this Feb 28, 2021
@Jeckerson
Copy link
Member

company.php

<?php

use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Db\Reference;
use Phalcon\Migrations\Mvc\Model\Migration;

/**
 * Class CompanyMigration_100
 */
class CompanyMigration_100 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('company', [
                'columns' => [
                    new Column(
                        'id',
                        [
                            'type' => Column::TYPE_CHAR,
                            'default' => "uuid_generate_v4()",
                            'notNull' => true,
                            'size' => 36,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'country_id',
                        [
                            'type' => Column::TYPE_CHAR,
                            'notNull' => true,
                            'size' => 2,
                            'after' => 'id'
                        ]
                    ),
                    new Column(
                        'name',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 100,
                            'after' => 'country_id'
                        ]
                    ),
                    new Column(
                        'title',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 100,
                            'after' => 'name'
                        ]
                    ),
                    new Column(
                        'logo',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => false,
                            'after' => 'title'
                        ]
                    ),
                    new Column(
                        'description',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => false,
                            'after' => 'logo'
                        ]
                    ),
                    new Column(
                        'externalurl',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => false,
                            'after' => 'description'
                        ]
                    ),
                    new Column(
                        'created_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "CURRENT_TIMESTAMP",
                            'notNull' => false,
                            'after' => 'externalurl'
                        ]
                    ),
                    new Column(
                        'updated_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "CURRENT_TIMESTAMP",
                            'notNull' => false,
                            'after' => 'created_at'
                        ]
                    ),
                    new Column(
                        'created_ip',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => false,
                            'size' => 40,
                            'after' => 'updated_at'
                        ]
                    ),
                    new Column(
                        'updated_ip',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => false,
                            'size' => 40,
                            'after' => 'created_ip'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('pk_company_id_0', ['id'], ''),
                    new Index('unq_company_country_id', ['country_id', 'name'], '')
                ],
                'references' => [
                    new Reference(
                        'fk_company_country',
                        [
                            'referencedSchema' => 'deliveryapp',
                            'referencedTable' => 'country',
                            'columns' => ['country_id'],
                            'referencedColumns' => ['id'],
                            'onUpdate' => 'NO ACTION',
                            'onDelete' => 'NO ACTION'
                        ]
                    )
                ],
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {

    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down()
    {

    }

}

country.php

<?php

use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Db\Reference;
use Phalcon\Migrations\Mvc\Model\Migration;

/**
 * Class CompanyMigration_100
 */
class CompanyMigration_100 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('company', [
                'columns' => [
                    new Column(
                        'id',
                        [
                            'type' => Column::TYPE_CHAR,
                            'default' => "uuid_generate_v4()",
                            'notNull' => true,
                            'size' => 36,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'country_id',
                        [
                            'type' => Column::TYPE_CHAR,
                            'notNull' => true,
                            'size' => 2,
                            'after' => 'id'
                        ]
                    ),
                    new Column(
                        'name',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 100,
                            'after' => 'country_id'
                        ]
                    ),
                    new Column(
                        'title',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 100,
                            'after' => 'name'
                        ]
                    ),
                    new Column(
                        'logo',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => false,
                            'after' => 'title'
                        ]
                    ),
                    new Column(
                        'description',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => false,
                            'after' => 'logo'
                        ]
                    ),
                    new Column(
                        'externalurl',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => false,
                            'after' => 'description'
                        ]
                    ),
                    new Column(
                        'created_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "CURRENT_TIMESTAMP",
                            'notNull' => false,
                            'after' => 'externalurl'
                        ]
                    ),
                    new Column(
                        'updated_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "CURRENT_TIMESTAMP",
                            'notNull' => false,
                            'after' => 'created_at'
                        ]
                    ),
                    new Column(
                        'created_ip',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => false,
                            'size' => 40,
                            'after' => 'updated_at'
                        ]
                    ),
                    new Column(
                        'updated_ip',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => false,
                            'size' => 40,
                            'after' => 'created_ip'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('pk_company_id_0', ['id'], ''),
                    new Index('unq_company_country_id', ['country_id', 'name'], '')
                ],
                'references' => [
                    new Reference(
                        'fk_company_country',
                        [
                            'referencedSchema' => 'deliveryapp',
                            'referencedTable' => 'country',
                            'columns' => ['country_id'],
                            'referencedColumns' => ['id'],
                            'onUpdate' => 'NO ACTION',
                            'onDelete' => 'NO ACTION'
                        ]
                    )
                ],
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {

    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down()
    {

    }

}

subscription.php

<?php

use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Db\Reference;
use Phalcon\Migrations\Mvc\Model\Migration;

/**
 * Class SubscriptionMigration_100
 */
class SubscriptionMigration_100 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('subscription', [
                'columns' => [
                    new Column(
                        'id',
                        [
                            'type' => Column::TYPE_CHAR,
                            'notNull' => true,
                            'size' => 36,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'plan_id',
                        [
                            'type' => Column::TYPE_CHAR,
                            'notNull' => false,
                            'size' => 36,
                            'after' => 'id'
                        ]
                    ),
                    new Column(
                        'company_id',
                        [
                            'type' => Column::TYPE_CHAR,
                            'notNull' => true,
                            'size' => 36,
                            'after' => 'plan_id'
                        ]
                    ),
                    new Column(
                        'name',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => false,
                            'size' => 100,
                            'after' => 'company_id'
                        ]
                    ),
                    new Column(
                        'description',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => false,
                            'after' => 'name'
                        ]
                    ),
                    new Column(
                        'days',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'default' => "31",
                            'notNull' => true,
                            'size' => 1,
                            'after' => 'description'
                        ]
                    ),
                    new Column(
                        'started_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "CURRENT_TIMESTAMP",
                            'notNull' => true,
                            'after' => 'days'
                        ]
                    ),
                    new Column(
                        'ended_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "CURRENT_TIMESTAMP",
                            'notNull' => true,
                            'after' => 'started_at'
                        ]
                    ),
                    new Column(
                        'cancelled_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'notNull' => false,
                            'after' => 'ended_at'
                        ]
                    ),
                    new Column(
                        'created_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "CURRENT_TIMESTAMP",
                            'notNull' => true,
                            'after' => 'cancelled_at'
                        ]
                    ),
                    new Column(
                        'updated_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "CURRENT_TIMESTAMP",
                            'notNull' => false,
                            'after' => 'created_at'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('pk_subscription_id', ['id'], ''),
                    new Index('unq_subscription', ['plan_id', 'company_id', 'started_at'], '')
                ],
                'references' => [
                    new Reference(
                        'fk_subscription_company',
                        [
                            'referencedSchema' => 'deliveryapp',
                            'referencedTable' => 'company',
                            'columns' => ['company_id'],
                            'referencedColumns' => ['id'],
                            'onUpdate' => 'NO ACTION',
                            'onDelete' => 'NO ACTION'
                        ]
                    ),
                    new Reference(
                        'fk_subscription_plan',
                        [
                            'referencedSchema' => 'deliveryapp',
                            'referencedTable' => 'plan',
                            'columns' => ['plan_id'],
                            'referencedColumns' => ['id'],
                            'onUpdate' => 'NO ACTION',
                            'onDelete' => 'NO ACTION'
                        ]
                    )
                ],
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {

    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down()
    {

    }

}

@yassinrais
Copy link
Author

yassinrais commented Mar 23, 2021

I think forging keys they will need to wait until all morphs tables endup in all migrations in one folder by order , after that , will excute all the forging keys in all migrations

@Jeckerson
Copy link
Member

@yassinrais It is another working approach, yes. But in DBs it is possible to temporary disable foreign keys, create structure and then re-enable them again. Unfortunately in pgsql it is slightly different and more complicated approach. By that it might be easier to generate new migrations without thinking how to ship them to the production.

@Jeckerson Jeckerson modified the milestones: 2.2.x, 3.0.x May 6, 2021
@Jeckerson
Copy link
Member

It is partially possible to achieve it via enabled skip-foreign-checks option. Auto detect of foreign keys atm is not in our plans... Closing.

Phalcon ecosystem board automation moved this from To Do to Done Aug 11, 2021
@Jeckerson Jeckerson removed this from the 3.0.x milestone Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working PostgreSQL
Projects
No open projects
Development

No branches or pull requests

3 participants