orm icon indicating copy to clipboard operation
orm copied to clipboard

Nullable BelongsTo relation generates a non-nullable inner key

Open vvval opened this issue 5 years ago • 2 comments

DB-first application

Having next annotations in the entity:

    /**
     * @Cycle\Relation\BelongsTo(
     *     target="Session",
     *     cascade=null,
     *     nullable=true,
     *     innerKey="session_id",
     *     outerKey="id",
     *     fkCreate=null,
     *     fkAction=null,
     *     indexCreate=null,
     *     load=null
     * )
     */
    public ?Session $session = null;

db schema has the next column declaration:

session_id int null

The migration contains the next column change:

    public function up()
    {
        $this->table('<tbl>')
            ->alterColumn('session_id', 'integer', [
                'nullable' => false,
                'default'  => null
            ])
            ->update();
    }

As a result: mysql exception (because the column is null and already has null-values)

Fixed in the code by declaring the column explicitly


    /**
     * @Cycle\Column(type="int", nullable=true)
     */
    private int $session_id;

Probably the bug is in the cycle/schema-builder rather than in the orm itself UPD, probably related to the bigInt column type

vvval avatar Sep 09 '20 07:09 vvval

On the latest version of the Cycle ORM this bug is not reproduced. @vvval can you confirm this?

My case:

/** @Entity() */
class A
{
    /** @Column(type="primary") */
    public $id;

    /**
     * @BelongsTo(
     *     target="B",
     *     cascade=null,
     *     nullable=true,
     *     innerKey="a_id",
     *     outerKey="id",
     *     fkCreate=null,
     *     fkAction=null,
     *     indexCreate=null,
     *     load=null
     * )
     */
    public $b;
}

/** @Entity() */
class B
{
    /** @Column(type="primary") */
    public $id;
}

#Migration:
class OrmDefault423fb365959c10734118b81d782d08be extends Migration
{
    protected const DATABASE = 'default';

    public function up(): void
    {
        $this->table('a')
            ->addColumn('a_id', 'integer', [
                'nullable' => true,
                'default'  => null
            ])
            ->addIndex(["a_id"], [
                'name'   => 'a_index_a_id_60643f6742c37',
                'unique' => false
            ])
            ->addForeignKey(["a_id"], 'b', ["id"], [
                'name'   => 'a_a_id_fk',
                'delete' => 'CASCADE',
                'update' => 'CASCADE'
            ])
            ->update();
    }

    public function down(): void
    {
        $this->table('a')
            ->dropForeignKey(["a_id"])
            ->dropIndex(["a_id"])
            ->dropColumn('a_id')
            ->update();
    }
}

roxblnfk avatar Mar 31 '21 09:03 roxblnfk

Can't be reproduced from scratch too @wolfy-j we might need your help, I can't remember all DB prerequisites according to this relation. You can check it in the project I've migrated to v2. It would be great if you paste here a tiny snippet of DB entities declaration (from project v1) and corresponding annotated properties (from project v2)

vvval avatar Jul 12 '21 14:07 vvval

The BelongsTo relation has been rewritten in ORM v2. If the bug is reproducible feel fre to open a new issue.

roxblnfk avatar Aug 03 '23 21:08 roxblnfk