msgphp / symfony-demo-app

A Symfony demo application with basic user management
https://github.com/msgphp/msgphp
MIT License
0 stars 0 forks source link

Foreign key error on staging servers #123

Closed jkwakman closed 4 years ago

jkwakman commented 4 years ago

Dear Roland,

On our staging servers we are hitting the following error when executing doctrine:schema:update --dump-sql:

In AbstractPlatform.php line 2597: Incomplete definition. 'foreign' required.

This only happens after a cache is build. If I remove the cache I am allowed to do a doctrine:schema:update --dump-sql. After a cache is build the error appears.

AbstractPlatform.php


   /**
     * Obtains DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
     * of a column declaration to be used in statements like CREATE TABLE.
     *
     * @return string
     *
     * @throws InvalidArgumentException
     */
    public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey)
    {
        $sql = '';
        if (strlen($foreignKey->getName())) {
            $sql .= 'CONSTRAINT ' . $foreignKey->getQuotedName($this) . ' ';
        }

        $sql .= 'FOREIGN KEY (';

        if (count($foreignKey->getLocalColumns()) === 0) {
            throw new InvalidArgumentException("Incomplete definition. 'local' required.");
        }

        if (count($foreignKey->getForeignColumns()) === 0) {
            throw new InvalidArgumentException("Incomplete definition. 'foreign' required.");
        }

        if (strlen($foreignKey->getForeignTableName()) === 0) {
            throw new InvalidArgumentException("Incomplete definition. 'foreignTable' required.");
        }

        return $sql . implode(', ', $foreignKey->getQuotedLocalColumns($this))
            . ') REFERENCES '
            . $foreignKey->getQuotedForeignTableName($this) . ' ('
            . implode(', ', $foreignKey->getQuotedForeignColumns($this)) . ')';
    }

The FK is the Foreign Key from the following query: ALTER TABLE premium_user ADD CONSTRAINT FK_B0390CB9BF396750 FOREIGN KEY (id) REFERENCES user (id) ON DELETE CASCADE;

If I remove the cache I can validate the schema so this looks fine. On our dev servers everything works as expected.

Do you have any idea what can be the problem? It seems that user (id) part of the FK cannot be generated / validated and stays empty. We have used the same entities from this demo repo (only placed it in a User subfolder).

Everything works as expected. Only validating or generating schema's throws this error on our staging and possibly production servers. Below I have listed the Entities.

If you could give us a small hint where to find the solution to this mystery that would be awesome!

Kind regards,

Jack Kwakman

App\Entity\User\User

<?php

declare(strict_types=1);

namespace App\Entity\User;

use Doctrine\ORM\Mapping as ORM;
use MsgPhp\Domain\Event\DomainEventHandler;
use MsgPhp\Domain\Event\DomainEventHandlerTrait;
use MsgPhp\Domain\Model\CanBeConfirmed;
use MsgPhp\Domain\Model\CanBeEnabled;
use MsgPhp\Domain\Model\CreatedAtField;
use MsgPhp\User\Credential\EmailPassword;
use MsgPhp\User\Model\AttributeValuesField;
use MsgPhp\User\Model\EmailPasswordCredential;
use MsgPhp\User\Model\EmailsField;
use MsgPhp\User\Model\ResettablePassword;
use MsgPhp\User\Model\RolesField;
use MsgPhp\User\User as BaseUser;
use MsgPhp\User\UserId;

/**
 * @ORM\Entity()
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discriminator", type="string")
 * @ORM\DiscriminatorMap({"user" = "User", "premium_user" = "PremiumUser"})
 */
class User extends BaseUser implements DomainEventHandler
{
    use CreatedAtField;
    use EmailPasswordCredential;
    use ResettablePassword;
    use CanBeEnabled;
    use CanBeConfirmed;
    use EmailsField;
    use RolesField;
    use AttributeValuesField;
    use DomainEventHandlerTrait;

    /** @ORM\Id() @ORM\Column(type="msgphp_user_id", length=191) */
    private $id;

    public function __construct(UserId $id, string $email, string $password)
    {
        $this->id = $id;
        $this->createdAt = new \DateTimeImmutable();
        $this->credential = new EmailPassword($email, $password);
        $this->confirmationToken = bin2hex(random_bytes(32));
    }

    public function getId(): UserId
    {
        return $this->id;
    }
}

App\Entity\User\PremiumUser


<?php

declare(strict_types=1);

namespace App\Entity\User;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 *
 * @final
 */
class PremiumUser extends User
{
}
jkwakman commented 4 years ago

Strange error disappeared after removing cache and reboot apache. Sorry for the inconvenience.

Keep up the good work!

Kind regards,

Jack Kwakman