Change relations

I have a parent records with several children. I want to change some of them to another parent. It seems easy, change the FK of the child and it’s done. I can change the FK by hand but servoy refuses to do it through a method.

var firstParent = '1234'
var second parent = '5678'

for(t=1; t <= forms.z_all_action_attachment.controller.getMaxRecordIndex(); t++)
	{
		// finds all attachment for the groups mailing	
		forms.z_all_action_attachment.controller.find()
		forms.z_all_action_attachment.act_att_rel = firstParent
		forms.z_all_action_attachment.controller.search()
		
		forms.z_all_action_attachment.act_att_rel = secondParent.
		databaseManager.saveData()
		
	}

It finds the firstParent’s children but it doesn’t change anything. If release the relation it works.

Am I doing something wrong or it’s a bug?[/code]

I don’t think youre code is wrong, maybe the secondKey has a wrong value?

You could try this code:

var firstParent = '1234'
var secondParent = '5678'

// finds all attachment for the groups mailing   
forms.z_all_action_attachment.controller.find()
forms.z_all_action_attachment.act_att_rel = firstParent
var _found = forms.z_all_action_attachment.controller.search()
var _record
for(t=1; t <= _found; t++)
{
	_record = forms.z_all_action_attachment.foundset.getRecord(t)
	_record.act_att_rel = secondParent
}
databaseManager.saveData()

I don’t think youre code is wrong, maybe the secondKey has a wrong value?

You could try this code:

var firstParent = '1234'
var secondParent = '5678'

// finds all attachment for the groups mailing   
forms.z_all_action_attachment.controller.find()
forms.z_all_action_attachment.act_att_rel = firstParent
var _found = forms.z_all_action_attachment.controller.search()
var _record
for(t=1; t <= _found; t++)
{
	_record = forms.z_all_action_attachment.foundset.getRecord(t)
	_record.act_att_rel = secondParent
}
databaseManager.saveData()

what do you mean?

maybe the secondKey has a wrong value?

The method can find the records to change but when it comes to change the key it just doesn’t do it.

The weird thing is that if I release the relation it changes the key!

Ok it works, but I had to be creative.

In the parent table my primary key is an integer. I had to create a text field that is filled by the Pk.

In the child table I’ve also created a text field. Then I have set my relation between both.

But it’ s still strange that an integer field can’ t be changed by a method.

irenem:
I have a parent records with several children. I want to change some of them to another parent. It seems easy, change the FK of the child and it’s done. I can change the FK by hand but servoy refuses to do it through a method.

var firstParent = '1234'

var second parent = ‘5678’

for(t=1; t <= forms.z_all_action_attachment.controller.getMaxRecordIndex(); t++)
{
// finds all attachment for the groups mailing
forms.z_all_action_attachment.controller.find()
forms.z_all_action_attachment.act_att_rel = firstParent
forms.z_all_action_attachment.controller.search()

	forms.z_all_action_attachment.act_att_rel = secondParent.
	databaseManager.saveData()
	
}



It finds the firstParent's children but it doesn't change anything. If release the relation it works.

Am I doing something wrong or it's a bug?[/code]

Don’t know if it’s a typo, but “second parent” != “secondParent”
;-)

PS: you definitely can change programmatically the value of an fk. I’m doing it every time…

Hi Riccardino,

It is definitely a typo, my actual code has different names.

PS: you definitely can change programmatically the value of an fk. I’m doing it every time…

Me to, It works with other tables that’s what makes me go nuts.

irenem:
Hi Riccardino,

It is definitely a typo, my actual code has different names.

PS: you definitely can change programmatically the value of an fk. I’m doing it every time…

Me to, It works with other tables that’s what makes me go nuts.

Don’t worry: we’ll find out ;-)

I see something strange in your code, but I can’t look into it right now.
Are you sure it’s ok?

When I go through it step by step with the debugger it does everything well but the last step:

forms.z_all_action_attachment.act_att_rel = secondParent.

I have pointed the same code to other two related tables and it works like heaven, so I think the code is ok.

It is like the table is corrupted, but I cannot imagine how.

I’ ve also took a look to the fields, they are exactly the sam as in my other two tables.

I can sceram! :x But Im glad with your help :lol:

saluti
Irene

irenem:
I can sceram! :x But Im glad with your help :lol:

:)

var firstParent = '1234'
var second parent = '5678'

for(t=1; t <= forms.z_all_action_attachment.controller.getMaxRecordIndex(); t++)
   {
      // finds all attachment for the groups mailing   
      forms.z_all_action_attachment.controller.find()
      forms.z_all_action_attachment.act_att_rel = firstParent
      forms.z_all_action_attachment.controller.search()
      
      forms.z_all_action_attachment.act_att_rel = secondParent.
      databaseManager.saveData()
      
   }

Let me get it straight:
you park in two vars the old and the new fk
then you loop through a fset searching all the related items
1st question: If you search the children, the find+search procedure should be enough, right?
So why do yuo need a loop? To set the new fk to all the children? If so, only the last two instructions

   forms.z_all_action_attachment.act_att_rel = secondParent.
      databaseManager.saveData()

Should be inside the loop… Or do I miss something?

2nd question: what happens after the search? Do you actually have a record selected?
Try to remove the loop and see if the new fk is assigned to the selected record.

Ciao

1st question: If you search the children, the find+search procedure should be enough, right?
So why do yuo need a loop? To set the new fk to all the children? If so, only the last two instructions

You are absolutely right. I tried that at first, since it didn’t replace anything I thought that it maybe would if it has to do it one by one. But unfortunately…

2nd question: what happens after the search? Do you actually have a record selected?
Try to remove the loop and see if the new fk is assigned to the selected record.

Yes, the search is ok and it selects the fist record, then nothing happens.

Since the very same code works well with other tables, I think I’ ll replace my table with another “fresh made” one. Lets see what happens!

I keep you infomed

Ok problem solved! I’ve replaced my database with a new wan and it works like sunshine.

What happened? No idea!

Anyway thanks to all for the help.

Irene