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
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
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;
}
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.
@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 :
i cannot guarantee you won t lost some keys but you don t drop your datas at all .
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.