Errore SQL eseguendo uno unit test

Ciao a tutti, questo è il mio primo post.

Ho un errore strano in una INSERT sql, ho seguito pedissequamente alcuni post per riuscire a stamparmi una istruzione di INSERT che mi genera un errore di violazione di chiave esterna (ma invece secondo me è corretta).

L’errore che mi viene mostrato da consolle quando eseguo il test è questo

[sql] CDbException: CDbCommand ha riportato un errore nell’esecuzione della query SQL: SQLSTATE[23000]: Integrity constraint violation:

1452 Cannot add or update a child row: a foreign key constraint fails (`reminder_test`.`reminder`, 


CONSTRAINT `fk_reminder_payment` FOREIGN KEY (`payment_id`) REFERENCES `payment` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)





/var/www/html/yii/framework/db/CDbCommand.php:336


/var/www/html/yii/framework/db/ar/CActiveRecord.php:1017


/var/www/html/yii/framework/db/ar/CActiveRecord.php:781


/var/www/html/reminder-yii/protected/tests/unit/ReminderTest.php:28


                                   


FAILURES!                          


Tests: 1, Assertions: 0, Errors: 1.[/sql]

Questo errore mi dice che ho una chiave esterna che se riferisce ad un record padre non esistente. Se mi faccio scrivere la query in un file leggo:

[sql]

2015/03/06 12:13:32 [error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[23000]: Integrity constraint violation: 


1452 Cannot add or update a child row: a foreign key constraint fails (`reminder_test`.`reminder`, CONSTRAINT `fk_reminder_payment` 


FOREIGN KEY (`payment_id`) REFERENCES `payment` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION). 


The SQL statement executed was: 


INSERT INTO `reminder` (`period`, `s_time`, `message`, `period_id`, `msgtype_id`, `payment_id`) 


VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5). 





Bound with :yp0=10, :yp1='1970-01-01', :yp2='Test Reminder 1', :yp3=1, :yp4=1, :yp5=1.

[/sql]

Ma i parametri ci sono tutti!

Se lancio l’istruzione visualizzata in MySQL Benchmark, l’insert viene effettuata senza errori:

1 row(s) affected

Qualcuno ha un’idea di dove stia sbagliando?

Alcuni dati:

Yii Version 1.1.8 June 26, 2011

PHP 5.5.9

Se serve altro ditemelo. Grazie!

Ciao e benvenuto!

Sembra che il valore di “payment_id” non sia presente nella tabella “payment”, cioè l’id che stai inserendo reminder.payment_id c’è tra i vari payment.id ?

Ciao a te e grazie del benvenuto.

Sì esatto, l’errore direbbe proprio che non c’è il record a cui si riferisce la chiave esterna reminder.payment_id = 1, ma il record id=1 nella tabella payment c’è! :mellow:

Ricontrollo e poi ti so dire, grazie

Svelato l’arcano.

Essendomi premurato di seguire le best practices che dicono di usare per i test un database diverso da quello di sviluppo, continuavo a guardare il database di SVILUPPO, mentre gli unit test li sto facendo nel db di TEST, nel quale nella tabella payment non avevo alcun record. :lol:

Addesso è tutto green:


PHPUnit 3.7.28 by Sebastian Bergmann.


Configuration read from /var/www/html/reminder-yii/protected/tests/phpunit.xml

.

Time: 115 ms, Memory: 7.00Mb


OK (1 test, 1 assertion)



In ogni caso, una cosa tutto questo me l’ha insegnata: come abilitare il logging su browser e su filesystem in Yii.

Scusate il disturbo…