I am trying to create a mysql trigger using MAMP for a joomla2.5 application.
My trigger:
CREATE TRIGGER `getDivision` BEFORE INSERT ON `encex_autoschedGames`
FOR EACH ROW BEGIN
select division into @div_a from encex_autoschedTeams where team_name = new.team_a;
set new.division = @div_a;
END
I keep getting the following error:
Column 'division' cannot be null
The story:
I have two tables: 1) Games 2) Teams.
Teams has columns ‘team_name’ and ‘division’
Games has coloumns ‘team_a’, ‘team_b’, and ‘division’
Sample Tables:
Teams table:
Team_name, Division
"team1","A"
"Team2","A"
"Team3","A"
"Team4","A"
"Team5","B"
"Team6","B"
"Team7","B"
"Team8","B"
For the games table, I want the division to come from the Teams table.. So when I insert Team1 Vs Team2 it auto fills the division as A.
Games Table:
Team_a, Team_b, Division
"Team1", "Team2", "A"
I’d like to create a trigger so that when a row is created in the games table it fills the division column with ‘division’ from the ‘Teams’ table where team_name = team_a.
Ideally I’ll build it so it ensures division is the same for both team_a and team_b, but I figure small steps are best here.
After some debugging, I believe my error is in the:
select division into @div_a from encex_autoschedTeams where team_name = new.team_a;
for some reason, the where clause is returning null. After some command line testing, it shouldn’t be. Maybe New.team_a isn’t right?
The original code works perfectly. The problem was due to to parsing a string earlier on in the code and leaving a difficult to see space in the string of team_names. The query was returning null because it wasn’t matching “team1″ to ” team1″.