1

i work with symfony on ubuntu 12.04, when i operate a doctrine schema update from command line, the dump always prints out that he has to make things like this:

ALTER TABLE melu_numeros ADD CONSTRAINT FK_8062E62AFB539063 FOREIGN KEY (id_fan) REFERENCES melu_fanzine (id_fan);
ALTER TABLE Image ADD CONSTRAINT FK_4FC2B5BABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES melu_numeros (id_num_fan);
CREATE INDEX IDX_4FC2B5BABEDD0E7 ON Image (id_num_fan);
DROP INDEX IDX_7B0247F15D172A78 ON melu_categories;
ALTER TABLE melu_categories ADD id_num_fan INT NOT NULL, DROP numero_id;
ALTER TABLE melu_categories ADD CONSTRAINT FK_7B0247F1ABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES melu_numeros (id_num_fan);
CREATE INDEX IDX_7B0247F1ABEDD0E7 ON melu_categories (id_num_fan);
ALTER TABLE melu_fichiers ADD CONSTRAINT FK_E2A048FCD6E65D60 FOREIGN KEY (id_agen) REFERENCES melu_agenda (id_agen);
ALTER TABLE melu_fanzine ADD CONSTRAINT FK_99AC1D58A656D38D FOREIGN KEY (id_asso) REFERENCES melu_association (id_asso);
ALTER TABLE melu_tarifs ADD CONSTRAINT FK_409B4C8D6E65D60 FOREIGN KEY (id_agen) REFERENCES melu_agenda (id_agen);
ALTER TABLE melu_newsletter CHANGE email email VARCHAR(50) NOT NULL;
ALTER TABLE melu_commentaire ADD CONSTRAINT FK_EE62B25DABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES melu_numeros (id_num_fan);
ALTER TABLE melu_contenus DROP INDEX FK_D9DCDC6130453DE8, ADD UNIQUE INDEX UNIQ_D9DCDC6130453DE8 (type_cont);
ALTER TABLE melu_contenus ADD CONSTRAINT FK_D9DCDC6130453DE8 FOREIGN KEY (type_cont) REFERENCES melu_types (id_types);
CREATE UNIQUE INDEX id_cont ON melu_contenus (id_cont);
ALTER TABLE melu_types ADD CONSTRAINT FK_8AB9AD2AB7B5C800 FOREIGN KEY (id_types) REFERENCES melu_contenus (type_cont);
ALTER TABLE melu_photos ADD CONSTRAINT FK_F5C79087D6E65D60 FOREIGN KEY (id_agen) REFERENCES melu_agenda (id_agen);
ALTER TABLE melu_jour ADD CONSTRAINT FK_85C738A2D6E65D60 FOREIGN KEY (id_agen) REFERENCES melu_agenda (id_agen);
DROP INDEX IDX_6C66C2856D2F0F48 ON melu_util_droit;
ALTER TABLE melu_util_droit DROP useur_id;
ALTER TABLE melu_util_droit ADD CONSTRAINT FK_6C66C28550EAE44 FOREIGN KEY (id_utilisateur) REFERENCES melu_utilisateurs (id_util);

and when i force the update, i have this error:

[Doctrine\DBAL\DBALException]                                                                                                   
An exception occurred while executing 'ALTER TABLE Image ADD CONSTRAINT FK_4FC2B5BABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES  melu_numeros (id_num_fan)':
SQLSTATE[HY000]: General error: 1005 Can't create table 'meluzineorg1.#sql-5cf_438' (errno: 150)

the 2 entites Image and melu_numero that provke the error http://pastie.org/9100202

how could i make this all work well ?

Ty Kayn
  • 719
  • 7
  • 24
  • What do you have in mapping for that table? Have you tried to run the dumped SQL in a MySQL tools like phpmyadmin? – Javad Apr 21 '14 at 20:32
  • i tried to run in the phpmyadmin SQL panel, and i have the same error, however, the line associated with the error is "ALTER TABLE Image ADD CONSTRAINT FK_4FC2B5BABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES melu_numeros (id); CREATE INDEX IDX_4FC2B5BABEDD0E7 ON Image (id_num_fan);" then i checked the entities definitions for thise relation, and nothing seems to be wrong. – Ty Kayn Apr 22 '14 at 11:32
  • here, a pastie for the two entites concerned http://pastie.org/9100202 – Ty Kayn Apr 22 '14 at 11:40
  • Check my provided answer hopefully it helps – Javad Apr 22 '14 at 14:22

3 Answers3

1

solved, i inherited data tables from an old bunch of myisam tables.

Doctrine seems to have issues with foreign keys in myisam engine, so i converted everything in innoDb and now there is no more jokes about schema update half done :)

thanks everyone

Ty Kayn
  • 719
  • 7
  • 24
0

error 150 suggests you have something wrong with your primary keys, one example might be that you changed what is the key in one table, bu you didnt change the constraint on another table

try reading through this answer as its similar(if not identical) MySQL: Can't create table (errno: 150)

Community
  • 1
  • 1
Bartłomiej Wach
  • 1,968
  • 1
  • 11
  • 17
  • these are my two entities concerned with the 150 error, i dont find anything wrong between Image (id_num_fan) and melu_numeros (id_num_fan) http://pastie.org/9100202 but the error is still [Doctrine\DBAL\DBALException] An exception occurred while executing 'ALTER TABLE Image ADD CONSTRAINT FK_4FC2B5BABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES melu_numeros (id_num_fan)': SQLSTATE[HY000]: General error: 1005 Can't create table 'meluzineorg1.#sql-5cf_438' (errno: 150) – Ty Kayn Apr 22 '14 at 11:43
  • /** * * @var integer * @ORM\Column(name="id_num_fan", type="integer", nullable=false ) */ private $idNumFan; This is not a primary key yet you use it in a constraint. – Bartłomiej Wach Apr 22 '14 at 11:52
0

The problem is in your mapping for img

/**
 * @var arrayCollection
 * @ORM\OneToMany(targetEntity="Tykayn\MeluzineBundle\Entity\Image", mappedBy="numero", cascade={"persist", "merge"})
 * @ORM\JoinColumn(name="id_num_fan", referencedColumnName="id_num_fan")
 */
private $img;

as you seee you set the refrencedColumnName as id_num_fan but in you mapping in Image entity you did not defined id_num_fan as key to be used a foreign key in the top entity
You can change the mapping in Image entity as below and remove the $id property (if is not used as foreign key in other entity) or you can change the top mapping as * @ORM\JoinColumn(name="id", referencedColumnName="id") because id is a key, too.

/**
 * @var integer 
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(name="id_num_fan", type="integer", nullable=false )
 */
private $idNumFan;
Javad
  • 4,339
  • 3
  • 21
  • 36
  • id_num_fan is not defined as a key? i thought i defined it in `Melunumeros /** * gros num de volume, correspond au nom de fichier uploadé pour couverture * @ORM\Column(name="id_num_fan", type="integer", nullable=false) * @ORM\GeneratedValue(strategy="AUTO") * @ORM\Id */ private $idNum;` with the annotation @ORM\Id. i can't change the image -> $idNumFan to be a generated value, i want many Image to one Melunumero, linked with id_num_fan – Ty Kayn Apr 23 '14 at 12:33
  • Though use the `id` instead; because as you know only the primary key can be foreign key in another table, whereas `$idNumFan` is not a primary key [http://www.w3schools.com/sql/sql_foreignkey.asp] – Javad Apr 23 '14 at 14:15