set_relation_n_n not working on add screen
- Single Page
Posted 22 August 2012 - 18:41 PM
I am using set_relation_n_n as:
$crud->set_relation_n_n('userid','user_device','user','device_id','user_id','email_address','priority');
The logic works fine in edit screen.
In add screen, I can see the list, but when I press on save after entering info in the form, it doesn't say on screen that the record is added. But if I go to list screen, there is record in the main table(device table) with use_id displaying null. Also there is no entry on joint table user_device.
But if I try this same thing from edit screen, i.e. selecting users and then click on update, the record gets entered in the joint table.
I don't understand why this logic doesn't work in add screen ???
Posted 22 August 2012 - 18:54 PM
Have you got any errors at the firebug? What the server responses when you click to the save button?
Posted 22 August 2012 - 21:06 PM
I didnt know about firbug. Just downloaded it. It's awesome.
So the error we get is:
<h1>A Database Error Occurred</h1>
<p>Error Number: 1452</p><p>Cannot add or update a child row: a foreign key constraint fails (`device_tracking`.`user_device`, CONSTRAINT `FK_user_device_device` FOREIGN KEY (`device_id`) REFERENCES `device` (`device_id`))</p><p>INSERT INTO `user_device` (`device_id`, `user_id`, `priority`) VALUES (0, '17', 0)</p>
of course there is Foreign key for device_id. That's how the relation table will refer to the main table right???
I have device_id as primary key in main table and then relation table has device_id column which is foreign key refering to device table.
Now when a user is on add screen, user is creating record for device table. hence the device_id is not present at that moment.
But then system should insert into device first and then should try to insert into relation table right?
I don't know where I am going wrong
Posted 22 August 2012 - 21:57 PM
I dont from where does this 1st zero comes in picture
even if i type any value for device_id
insert statement shows 1st parameter as 0
Posted 22 August 2012 - 21:58 PM
still not working
Posted 23 August 2012 - 00:54 AM
Can you make sure that all of your tables (including user_device) have auto-increment primary key?
Posted 23 August 2012 - 05:17 AM
By the way, what versions of CodeIgniter and grocery CRUD do you use?
Posted 23 August 2012 - 16:23 PM
As of now the main table i.e. device is not having auto increment primary key
Since I want user himself to enter the device id on the same page, I dont have it as auto increment primary key.
while the third table i.e. user has auto increment primary key.
Also the joint table has auto increment primary key.
I will try with Auto increment primary key on main table and will let you guys know
Thanks a lot once again
Posted 23 August 2012 - 22:44 PM
instead I wrote 2 insert statements in callback function
and now I first insert into device table and then in user_device table.
Thanks a lot for your replies everyone
Posted 24 August 2012 - 04:09 AM
Posted 24 August 2012 - 18:08 PM
Even this thing works fine