Symfony2 Doctrine schema update fails

前端 未结 5 1198
不知归路
不知归路 2021-02-07 11:53

I created database on my local machine. After moving my project to server I imported backup from local (because I had some important data there).

Now,when I\'m trying t

相关标签:
5条回答
  • 2021-02-07 12:36

    you need to add a value related to the field in the new table.

    For example: if you have table A and B, and B is have the key of A (a_id) then you need to add a field in A with id = 1 and in the table B the a_id need to be changed to a value from A table - 1 in this case (make this for all the fields).

    After that run : php app/console doctrine:schema:update --force

    Regards

    0 讨论(0)
  • 2021-02-07 12:46

    For me it worked with the following setup (with two relations). The trick was to not mix up mappedBy and inversedBy.

    /**
     * @ORM\Entity
     * @ORM\Table(name="user")
     */
    class User extends BaseUser
    {
        /**
         * @var Merchant
         *
         * @ORM\OneToOne(targetEntity="Merchant", mappedBy="user")
         */
        protected $merchant;
    
        /**
         * @var Client
         *
         * @ORM\OneToOne(targetEntity="Client", mappedBy="user")
         */
        protected $client;
    }
    
    /**
     * @ORM\Table(name="merchant")
     * @ORM\Entity
     */
    class Merchant extends BaseEntity
    {
        /**
         * @ORM\OneToOne(targetEntity="User", inversedBy="merchant")
         */
        protected $user;
    }
    
    /**
     * @ORM\Table(name="client")
     * @ORM\Entity
     */
    class Client extends BaseEntity
    {
        /**
         * @ORM\OneToOne(targetEntity="User", inversedBy="client")
         */
        protected $user;
    }
    
    0 讨论(0)
  • 2021-02-07 12:48

    Schema update with foreign-key checks disabled

    If Doctrine schema updates fail because of foreign-key constraints, you simply need to disable foreign-key checks for this particular update.

    As a one-liner you can run:

    mysql -e "set foreign_key_checks = 0; `app/console doctrine:schema:update --dump-sql`"
    

    This prepends set foreign_key_checks = 0; to the output of app/console doctrine:schema:update --dump-sql so it actually calls exactly what Doctrine would call but with foreign-key checks disabled. Configure the mysql call to your needs.

    Your schema is updated and depending on your changes no data is lost.

    Keep in mind that sometimes the order of the queries is important and Doctrine simply didn't order them right. In this case you have to order the queries correctly by your own and then use that ordered list of queries instead.

    0 讨论(0)
  • 2021-02-07 12:50

    @maxian

    Michael Villeneuve answer is not totally right. In case of a production environnement or kind of , you just can t drop schema and recreate it.

    The only way to perform it on your current schema is by the followings :

    1. php app/console doctrine:schema:update --dump-sql . Copy the ouptut. Its the direct SQL queries to update your schema
    2. connect mysql with mysql command line or through a mysql client
    3. Disable foreign keys checking by call this query : "set foreign_key_checks=0;"
    4. put the queries from doctrine:schema:update
    5. Enable back foreign key checking with : "set foreign_key_checks=1;"

    i cannot guarantee you won t lost some keys but you don t drop your datas at all .

    0 讨论(0)
  • 2021-02-07 12:52

    Your problem is that you want to modify a table with existing constraint. I see two solutions:

    If you are in dev, you can rebuild your database

    doctrine:database:drop --force
    doctrine:database:create
    doctrine:schema:create
    

    If you're in production it's a little more complicated.

    One solution I see is that you could create a command to save your data, delete the data in the tables you want to alter, modify your schema, reload the data once your table is altered. Depending on the changes, it shouldn't take more then 2-3 hours. Just make sure you have a backup in case your command goes south.

    0 讨论(0)
提交回复
热议问题