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

One-To-One Self-referencing is being ignored for PostgreSQL #488

Open
fabpico opened this issue Feb 6, 2023 · 3 comments
Open

One-To-One Self-referencing is being ignored for PostgreSQL #488

fabpico opened this issue Feb 6, 2023 · 3 comments

Comments

@fabpico
Copy link

fabpico commented Feb 6, 2023

"doctrine/doctrine-migrations-bundle": "^3.2"

According to https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/association-mapping.html#one-to-one-self-referencing

there is an example

<?php
#[Entity]
class Student
{
    // ...

    /** One Student has One Mentor. */
    #[OneToOne(targetEntity: Student::class)]
    #[JoinColumn(name: 'mentor_id', referencedColumnName: 'id')]
    private Student|null $mentor = null;

    // ...
}

When I test this in my new entity

<?php declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity()]
#[ORM\Table(name: '`pages`')]
class Page
{
    #[ORM\Id]
    #[ORM\Column(type: "uuid")]
    private string $id;

    #[ORM\Column]
    private string $title;

    #[ORM\Column]
    private string $content;

    #[OneToOne(targetEntity: Page::class)]
    #[JoinColumn(name: 'parent_id', referencedColumnName: 'id')]
    private Page|null $parent = null;
}

Then I do php bin/console doctrine:migrations:diff.

The result is

<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20230206151152 extends AbstractMigration
{
    public function getDescription(): string
    {
        return '';
    }

    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE TABLE "pages" (id UUID NOT NULL, title VARCHAR(255) NOT NULL, content VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('COMMENT ON COLUMN "pages".id IS \'(DC2Type:uuid)\'');
    }

    public function down(Schema $schema): void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE SCHEMA public');
        $this->addSql('DROP TABLE "pages"');
    }
}

No foreign key or anything for parent is created.

@stof
Copy link
Member

stof commented Feb 6, 2023

You imported the namespace as an ORM prefix in your use statement but you haven't use ORM\OneToOne or a use statement for the OneToOne attribute. So your OneToOne attribute is currently a App\Entity\OneToOne, which is not known by Doctrine (and probably does not exist).

@fabpico
Copy link
Author

fabpico commented Feb 7, 2023

@stof You are right, but then I would expect an error message that App\Entity\OneToOne was not found.

But I just get

Generated new migration class to "/var/www/html/migrations/Version20230207113436.php"
 
 To run just this migration for testing purposes, you can use migrations:execute --up 'DoctrineMigrations\\Version20230207113436'
 
 To revert the migration you can use migrations:execute --down 'DoctrineMigrations\\Version20230207113436'

I suggest to let throw an error if a specified attribute class was not found.

@stof
Copy link
Member

stof commented Feb 16, 2023

PHP allows attribute to use non-existent classes as long as you don't try to instantiate them.
And doctrine/orm won't try to instantiate attributes that are not its own.

However, static analysis tools like Psalm and phpstan (or your IDE) generally report non-existent classes used as attribute as being a mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants