Welcome Guest, Not a member yet? Register   Sign In
Datamapper ORM in-table foreign key not working
#1

[eluser]DaMaul[/eluser]
For some reason Datamapper is not recognising my in-table foreign key relationship and is attempting to use a joining table instead when I try and access my related object.

It's probably the simplest example of such a relationship you could get. Here's what my code looks like:

Code:
class Venue extends DataMapper
{
    public $has_one = ['town'];
}

class Town extends DataMapper
{
    public $has_many =['venue'];
}

// Controller
class Pubs extends CI_Controller
{
    public function single($id) {
        $v = new Venue($id);
        $v->town->get();
        echo $v->town->name;
    }
}

Here's the mysql CREATE code for my venues table:
Code:
CREATE TABLE `venues` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL DEFAULT NULL,
`town_id` INT(10) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `town_venue` (`town_id`),
CONSTRAINT `FK1_towns` FOREIGN KEY (`town_id`) REFERENCES `towns` (`id`)
)

And here for the towns table:
Code:
CREATE TABLE `towns` (
`id` INT(10) UNSIGNED NOT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
`country` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)


When I run the method in the controller, I get a codeigniter DB error stating "Table 'mydatabase.towns_venues' doesn't exist".

Obviously datamapper hasn't realised that it can use an in-table foreign key and is looking for a joining table. Can anyone suggest why this is happening? I'm pulling my hair out trying to figure out what's wrong with my setup


Messages In This Thread
Datamapper ORM in-table foreign key not working - by El Forum - 06-29-2014, 01:35 PM
Datamapper ORM in-table foreign key not working - by El Forum - 06-29-2014, 03:24 PM
Datamapper ORM in-table foreign key not working - by El Forum - 06-29-2014, 04:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB