Nullable BelongsTo relation generates a non-nullable inner key
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
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();
}
}
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)
The BelongsTo relation has been rewritten in ORM v2. If the bug is reproducible feel fre to open a new issue.