⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

set_relation_n_n not working on add screen



ashish_nirkhe
  • profile picture
  • Member

Posted 22 August 2012 - 18:41 PM

Hi,
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 ???

noskov.biz
  • profile picture
  • Member

Posted 22 August 2012 - 18:54 PM

Hi, ashish_nirkhe and welcome to the forum!
Have you got any errors at the firebug? What the server responses when you click to the save button?

ashish_nirkhe
  • profile picture
  • Member

Posted 22 August 2012 - 21:06 PM

hi, Thanks for your reply
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 :(

ashish_nirkhe
  • profile picture
  • Member

Posted 22 August 2012 - 21:57 PM

also in insert statement above (i.e. response from server) has values(0,'17',0)
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

ashish_nirkhe
  • profile picture
  • Member

Posted 22 August 2012 - 21:58 PM

I even treid calling callback_before_insert and tried inserting into main table in this function
still not working :(

goFrendiAsgard
  • profile picture
  • Member

Posted 23 August 2012 - 00:54 AM

Seems there is a problem with the database.

Can you make sure that all of your tables (including user_device) have auto-increment primary key?

noskov.biz
  • profile picture
  • Member

Posted 23 August 2012 - 05:17 AM

ashish_nirkhe, please post here the dump of those tables and also the controller

By the way, what versions of CodeIgniter and grocery CRUD do you use?

ashish_nirkhe
  • profile picture
  • Member

Posted 23 August 2012 - 16:23 PM

Thanks for your reply.
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 :)

ashish_nirkhe
  • profile picture
  • Member

Posted 23 August 2012 - 22:44 PM

ok so the problem seems to be solved now. I didn't change the primary key to auto increment
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 :)

noskov.biz
  • profile picture
  • Member

Posted 24 August 2012 - 04:09 AM

[b] ashish_nirkhe, you are welcome :) [/b]

ashish_nirkhe
  • profile picture
  • Member

Posted 24 August 2012 - 18:08 PM

Also I tried putting auto increment primary key on device table and no extra functions.
Even this thing works fine :D