/** Test foreign key constraint sql generation */ void tst_QDjangoMetaModel::testConstraints() { QStringList sql; if (QDjango::database().driverName() == QLatin1String("QPSQL")) { sql << QLatin1String("CREATE TABLE \"tst_fkconstraint\" (" "\"id\" serial PRIMARY KEY, " "\"noConstraint_id\" integer NOT NULL REFERENCES \"user\" (\"id\") DEFERRABLE INITIALLY DEFERRED, " "\"cascadeConstraint_id\" integer NOT NULL REFERENCES \"user\" (\"id\") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, " "\"restrictConstraint_id\" integer NOT NULL REFERENCES \"user\" (\"id\") ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED, " "\"nullConstraint_id\" integer REFERENCES \"user\" (\"id\") ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED" ")"); sql << QLatin1String("CREATE INDEX \"tst_fkconstraint_f388fc3c\" ON \"tst_fkconstraint\" (\"noConstraint_id\")"); sql << QLatin1String("CREATE INDEX \"tst_fkconstraint_4634d592\" ON \"tst_fkconstraint\" (\"cascadeConstraint_id\")"); sql << QLatin1String("CREATE INDEX \"tst_fkconstraint_728cefe1\" ON \"tst_fkconstraint\" (\"restrictConstraint_id\")"); sql << QLatin1String("CREATE INDEX \"tst_fkconstraint_44c71620\" ON \"tst_fkconstraint\" (\"nullConstraint_id\")"); } else if (QDjango::database().driverName() == QLatin1String("QMYSQL") || QDjango::database().driverName() == QLatin1String("QMYSQL3")) { sql << QLatin1String("CREATE TABLE `tst_fkconstraint` (" "`id` integer NOT NULL PRIMARY KEY AUTO_INCREMENT, " "`noConstraint_id` integer NOT NULL, " "`cascadeConstraint_id` integer NOT NULL, " "`restrictConstraint_id` integer NOT NULL, " "`nullConstraint_id` integer, " "CONSTRAINT `FK_noConstraint_id_8049d4ec` FOREIGN KEY (`noConstraint_id`) REFERENCES `user` (`id`), " "CONSTRAINT `FK_cascadeConstraint_id_d2686b82` FOREIGN KEY (`cascadeConstraint_id`) REFERENCES `user` (`id`) ON DELETE CASCADE, " "CONSTRAINT `FK_restrictConstraint_id_99b64be3` FOREIGN KEY (`restrictConstraint_id`) REFERENCES `user` (`id`) ON DELETE RESTRICT, " "CONSTRAINT `FK_nullConstraint_id_b4eac280` FOREIGN KEY (`nullConstraint_id`) REFERENCES `user` (`id`) ON DELETE SET NULL" ")"); sql << QLatin1String("CREATE INDEX `tst_fkconstraint_f388fc3c` ON `tst_fkconstraint` (`noConstraint_id`)"); sql << QLatin1String("CREATE INDEX `tst_fkconstraint_4634d592` ON `tst_fkconstraint` (`cascadeConstraint_id`)"); sql << QLatin1String("CREATE INDEX `tst_fkconstraint_728cefe1` ON `tst_fkconstraint` (`restrictConstraint_id`)"); sql << QLatin1String("CREATE INDEX `tst_fkconstraint_44c71620` ON `tst_fkconstraint` (`nullConstraint_id`)"); } else { sql << QLatin1String("CREATE TABLE \"tst_fkconstraint\" (" "\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT, " "\"noConstraint_id\" integer NOT NULL REFERENCES \"user\" (\"id\"), " "\"cascadeConstraint_id\" integer NOT NULL REFERENCES \"user\" (\"id\") ON DELETE CASCADE, " "\"restrictConstraint_id\" integer NOT NULL REFERENCES \"user\" (\"id\") ON DELETE RESTRICT, " "\"nullConstraint_id\" integer REFERENCES \"user\" (\"id\") ON DELETE SET NULL" ")"); sql << QLatin1String("CREATE INDEX \"tst_fkconstraint_f388fc3c\" ON \"tst_fkconstraint\" (\"noConstraint_id\")"); sql << QLatin1String("CREATE INDEX \"tst_fkconstraint_4634d592\" ON \"tst_fkconstraint\" (\"cascadeConstraint_id\")"); sql << QLatin1String("CREATE INDEX \"tst_fkconstraint_728cefe1\" ON \"tst_fkconstraint\" (\"restrictConstraint_id\")"); sql << QLatin1String("CREATE INDEX \"tst_fkconstraint_44c71620\" ON \"tst_fkconstraint\" (\"nullConstraint_id\")"); } // create tables QDjangoMetaModel userModel = QDjango::registerModel<User>(); QCOMPARE(userModel.createTable(), true); QDjangoMetaModel metaModel = QDjango::registerModel<tst_FkConstraint>(); QCOMPARE(metaModel.createTableSql(), sql); QCOMPARE(metaModel.createTable(), true); QDjango::setDebugEnabled(false); // drop tables QCOMPARE(metaModel.dropTable(), true); QCOMPARE(userModel.dropTable(), true); }
void cleanup() { const QDjangoMetaModel metaModel = QDjango::registerModel<T>(); QCOMPARE(metaModel.dropTable(), true); }