Erich
Erich

Reputation: 545

Symfony entities are not updated in database after changes

My symfony app is using Doctrine to persist entities in mysql.

Today I updated my entities "Advertiser" and "Report" so there are relations between the two - as suggested in this post: When using EntityType to show a select, can't save entity from Symfony form

When I try creating a migration, it says that the database is already in sync.

php bin/console make:migration

Returns: [WARNING] No database changes were detected. The database schema and the application mapping information are already in sync.

However if I look at the table for the report, I see it still has the old schema:

+---------------+------------+------+-----+---------+----------------+
| Field         | Type       | Null | Key | Default | Extra          |
+---------------+------------+------+-----+---------+----------------+
| id            | int(11)    | NO   | PRI | NULL    | auto_increment |
| advertiser_id | int(11)    | NO   | MUL | NULL    |                |
| start_date    | date       | NO   |     | NULL    |                |
| end_date      | date       | NO   |     | NULL    |                |
| deleted       | tinyint(1) | NO   |     | NULL    |                |
+---------------+------------+------+-----+---------+----------------+

Even though my entity looks like this now:

class Report
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="date")
     */
    private $start_date;

    /**
     * @ORM\Column(type="date")
     */
    private $end_date;

    /**
     * @ORM\Column(type="boolean")
     */
    private $deleted;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Advertiser", inversedBy="reports")
     * @ORM\JoinColumn(nullable=false)
     */
    private $advertiser;

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

    public function getStartDate(): ?\DateTimeInterface
    {
        return $this->start_date;
    }

    public function setStartDate(\DateTimeInterface $start_date): self
    {
        $this->start_date = $start_date;

        return $this;
    }

    public function getEndDate(): ?\DateTimeInterface
    {
        return $this->end_date;
    }

    public function setEndDate(\DateTimeInterface $end_date): self
    {
        $this->end_date = $end_date;

        return $this;
    }


    public function getDeleted(): ?bool
    {
        return $this->deleted;
    }

    public function setDeleted(bool $deleted): self
    {
        $this->deleted = $deleted;

        return $this;
    }

    public function getAdvertiser(): ?Advertiser
    {
        return $this->advertiser;
    }

    public function setAdvertiser(?Advertiser $advertiser): self
    {
        $this->advertiser = $advertiser;

        return $this;
    }
}

I've been searching for solutions and have tried these, but no luck:

php bin/console doctrine:cache:clear-metadata
php bin/console doctrine:schema:update --force

Please let me know if you have any suggestion on how I can update my database with my updated schema.

Upvotes: 2

Views: 9618

Answers (5)

Tomás Cot
Tomás Cot

Reputation: 1013

Maybe it's useful for someone, but when using annotations make sure that the comment block follows the DocBlock format, the first line should have two asterisks: /** and not a single asterisk /*

DocBlock

/**
*
*/

PHP Multiline comment

/*
*
*
*/

Upvotes: 1

Ian M
Ian M

Reputation: 66

I had the same issue, try clearing the cache with:

symfony console cache:clear

Upvotes: 3

Antoine Perry
Antoine Perry

Reputation: 29

As a many to one relation, it's normal that you're database advertiser column only stores the key of the report as a "link" to it, so that's why Symfony doesn't see any changes in your DB.

Maybe you can also use :

php bin/console doctrine:schema:update --dump-sql 

to see changes in your DB

php bin/console doctrine:schema:update --force 

to apply changes without using migrations

Upvotes: 1

Erich
Erich

Reputation: 545

I believe that the problem was that I previously had a property called "advertiser_id" (int) on the report object. And possibly I was trying to change too many things at once for doctrine to manage. Previously, I had tried to remove the advertiser_id while adding the relation property for advertiser.

To get doctrine working again, I removed the advertiser property from the Report object - along with the getters and setters. I also removed the reverse lookup stuff from the Advertiser object. When I tried to run the migration, it seems like there were several migrations that it was trying to run - all doing the same thing: dropping a foreign key that doesn't exist. So I commented out all of those commands in the migration files and finally was able to get it to migrate. I also removed the advertiser_id property. The app is working again.

Then I tried adding the "advertiser" relation property back to the report. This time it worked as expected and I was able to migrate. So I think the issue is related to my object already having an advertiser_id property. Now that I've added the advertiser relation to the Report object, I see that doctrine added an advertiser_id column to the table. I suspect that it being present previously was the reason things broke down.

Thanks for the replies! Glad to have it working again.

Upvotes: 0

Vytenis Ščiukas
Vytenis Ščiukas

Reputation: 473

Try using proper annotation setup may be missing required configuration, you can always try to validate your schema with bin/console doctrine:schema:validate:

/**
 * @ManyToOne(targetEntity="App\Entity\Advertiser", inversedBy="reports")
 * @JoinColumn(name="advertiser_id", referencedColumnName="id")
 */
private $advertiser;

And check Advertiser entity for issues as well, maybe it is missing primary key or something.

Upvotes: 0

Related Questions