views:

62

answers:

3

I'm using Codeigniters Active record library to carry out an update on a column on my DB.

Here's the SQL for the table

CREATE TABLE `schedules` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`reservation_id` INT(11) NULL DEFAULT NULL,
`title` VARCHAR(255) NOT NULL,
`description` VARCHAR(512) NULL DEFAULT NULL,
`start_date` DATE NOT NULL,
`start_time` TIME NOT NULL,
`end_time` TIME NULL DEFAULT NULL,
`enabled` TINYINT(1) NULL DEFAULT '1',
`status` ENUM('OPEN','RESERVED') NULL DEFAULT 'OPEN',
PRIMARY KEY (`id`),
INDEX `fk_schedules_reservations` (`reservation_id`),
CONSTRAINT `fk_schedules_reservations` FOREIGN KEY (`reservation_id`) REFERENCES `reservations` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION

)

I've declared reservation_id as nullable (reservation_id INT(11) NULL DEFAULT NULL)

The problem is that CI doesn't seem to want to send a NULL value when I create the statement.

$data['status'] = $this->Schedule->get_status_open();
$data['reservation_id'] = null; 
$this->Schedule->update($s_id, $data);

That bit of code just generates the following error message

Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (`ethyme/schedules`, CONSTRAINT `fk_schedules_reservations` FOREIGN KEY (`reservation_id`) REFERENCES `reservations` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

UPDATE `schedules` SET `status` = 'OPEN', `reservation_id` = '' WHERE `id` = '4'

From what I know, all you have to do is set the value to NULL and AR should pick up that it's a NULL value, but that doesn't seem to be the case here. Just keeps sending empty values.

I'm new to CI, do I have to do anything else to make it work? any ideas?

A: 

Your reservations_id column references an id column in a reservations table, right? Have you checked that the column in the reservstions table is also allowed to have a null value? If its the primary key and auto-incremented then that could well be the problem. I dont think you can have a column as null in one table if its designated as a foreign key to a column that cant be null in the referenced table.  Have i explained that clearly for you?

musoNic80
That's not the problem because I ran the query manually with null instead of quotes and it worked fine. I just need to actually be able to send the null value instead of the blank. Short of turning this into a custom query, nothing comes to mind.
jomanlk
+1  A: 

In CI the keyword NULL always needs to be uppercase. Try this simple change and see if that works. See the relevant bit of the userguide here.

musoNic80
nope, I had tried that earlier as well, still won't work. Since it's a null datatype, I don't think the case would have made much difference anyway.
jomanlk
That is part of the style guide and makes no difference to anything whatsoever.
Phil Sturgeon
Thought so, but worth a try!
musoNic80
+1  A: 

Try:

$this->db->set('reservation_id', NULL);

Could well be the $data approach doesn't like NULLs.

Phil Sturgeon
excellent! that worked. Sadly this means I won't be able to use the $data approach unless I work in an intermediary method that iterates through the array and sets the data. Anyway, problem solved. Thanks for your help everyone
jomanlk
did a bit more digging, turns out xss_clean was munching up my data. Added an exclusion list for data types so it would ignore NULL, TRUE, FALSE. Looks like the usage was correct after all :/
jomanlk
Yeah I figured it might be a bug, but I knew that way to work. Can you submit the bug to the CodeIgniter Issue Tracker on BitBucket and I'll try to get a fix into the core. Also, you can mix $data and ->set() as much as you like.
Phil Sturgeon