This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I'm struggling with getting my relations working.
When I try to open my backend form, containing a 'hasmany' relation, I get a 'Column not found' error. I've tried to specify a custom key but it's being ignored somehow. What am I missing?
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'myplugin_results_categories.race_id' in 'where clause'
The relation is that a race has many categories:
class Race extends Model
{
public $hasMany = [
'categories' => 'Myplugin\Results\Models\Category', 'key' => 'races_id'
];
}
In the hasMany definition above you used "races_id" (plural) whilein the error it is "race_id"
Hi Marc, thank you for replying!
Yes I'm aware that I didn't name that column in my database correctly. However, according to the documentation you're supposed to be able to compensate for that by passing the key
parameter in the relationship definition.
I have specified the (foreign) key according to the documentation but it still doesn't work since I get the same error message.
It should be an array when you have any extra parameters needed to add
public $hasMany = [
'categories' => [
'Myplugin\Results\Models\Category',
'key' => 'races_id'
]
];
Hi dtroy,
already did that (see the snippet in my first post), but it doesn't make any difference when I reload the plugin page, I get the same error.
linusbostrom13155 said:
Hi dtroy,
already did that (see the snippet in my first post), but it doesn't make any difference when I reload the plugin page, I get the same error.
The snippet in your first post isn't an array like @dtroy suggested.
The $hasMany
property is an array - but the value of 'categories'
isn't an array.
What you've got...
public $hasMany = [
'categories' => 'Myplugin\Results\Models\Category', 'key' => 'races_id'
];
What you should have.
public $hasMany = [
'categories' => ['Myplugin\Results\Models\Category', 'key' => 'races_id']
];
Worked like a charm! I cannot understand how I could miss that bracket after staring at the code for so long.
Thank you so much for taking the time to explain neilcarpenter and dtroy!
1-8 of 8