Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/Database/Table/bugs/bug170.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Test: bug 170
* @dataProvider? ../../databases.ini mysql
*/

declare(strict_types=1);

use Nette\Database\Table\ActiveRow;
use Tester\Assert;

require __DIR__ . '/../../connect.inc.php';

Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-bug170.sql");

Assert::noError(function () use ($context){
// this bug is about picking the right foreign key to specified table regardless FKs definition order
$context->table('Operator1')->where('country.id')->count();
$context->table('Operator2')->where('country.id')->count();
});
35 changes: 35 additions & 0 deletions tests/Database/files/mysql-bug170.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
DROP DATABASE IF EXISTS nette_test;
CREATE DATABASE nette_test;
USE nette_test;

CREATE TABLE `Country` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

CREATE TABLE `Region` (
`order` int(1) unsigned NOT NULL,
`countryId` int(4) unsigned NOT NULL,
PRIMARY KEY (`countryId`, `order`),
CONSTRAINT `Region_ibfk_2` FOREIGN KEY (`countryId`) REFERENCES `Country` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

CREATE TABLE `Operator1` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`countryId` int(4) unsigned NOT NULL,
`regionOrder` int(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `Operator_ibfk_1` FOREIGN KEY (`countryId`, `regionOrder`) REFERENCES `Region` (`countryId`, `order`),
CONSTRAINT `Operator_ibfk_2` FOREIGN KEY (`countryId`) REFERENCES `Country` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

CREATE TABLE `Operator2` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`countryId` int(4) unsigned NOT NULL,
`regionOrder` int(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `Operator2_ibfk_1` FOREIGN KEY (`countryId`) REFERENCES `Country` (`id`),
CONSTRAINT `Operator2_ibfk_2` FOREIGN KEY (`countryId`, `regionOrder`) REFERENCES `Region` (`countryId`, `order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

-- NOTE: Order of foreign keys to tables Region and Country is reversed in Operator2 table compared to Operator1 table