Getting error while updating foreign key of a table with unique index - ruby-on-rails

There are some duplicates in a table say tableA, however of id of tableA is a foreign key in another table. So before deleting the records in tableA I would like to update the foreign key in this other table. This is the schema of the other table.
Table Schema:
id
foriegn_key_id
attribute_id
Indexes:
"index_table_on_attribute_id_and_foriegn_key_id" UNIQUE, btree (attribute_id, foriegn_key_id)
Sample data:
id foriegn_key_id attribute_id
1 2 3
2 1 3
3 1 4
4 1 5
Below is the code that I tried to update the foreign key.
Table.where(foriegn_key_id: 1).update(foriegn_key_id: 2)
However, when the try the above code I get the following error.
ActiveRecord::RecordNotUnique
For the records I am getting the error alone I would like to have it deleted.
Is looping through and updating individual record and deleting the record that gets ActiveRecord::RecordNotUnique the only way or is there a simpler way?

Related

duplicate key value violates unique constraint. But there is no records in that table with the same details

I have a table with email as unique constraint. But when I try to insert the values. It showing unique constraint error, even there is no data in the table. If I try delete query it returns 0 results. I'm using PostgreSQL 12

Sequelize : how to join on foreign keys

I'm using Sequelize and Node.js, and I need to join 2 tables on 2 foreign keys,
tableA.hasOne(tableB, { foreignKey: 'fk_tableB' });
tableB.belongsTo(tableA, {foreignKey: 'fk_tableB' });
(by the way, I don't understand the functionality of "targetKey")
but I can only obtain a join on tableA.primaryKey = tableB.fk_tableB.
How can I replace the tableA.primaryKey by tableA.fk_tableA?
I also tried to define twice tableA : in 2 different structures (one with the real primary key and the other with fk_tableA as primary key), but it's also not working (because I need the real tableA mode in another place).
Has someone an idea? Is it a bug from Sequelize?
How can I replace the tableA.primaryKey by tableA.fk_tableA?
There is no tableA.fk_tableA. But if there were, we would expect you to have named it that because column tableA.fk_tableA is a FK to a key column in tableA. Because that's the convention for naming a column fk_tableA. Similarly we would expect a belongTo like yours that adds a column that is a FK to the tableA PK to call it fk_tableA, not fk_tableB. Just like your hasOne gives tableA a column fk_tableB to the tableB PK. (If you want a FK to be to some other column than the PK then you say so via targetKey.)
If you so named FKs after their target table, you seem to want tableA.fk_tableB = tableB.fk_tableA. The way you have named them now, you seem to want tableA.fk_tableB = tableB.fk_tableB.
I need to join 2 tables on 2 foreign keys
It is extremely unlikely that you need the join above. Declaring a column to be a FK says that a value of the source/referencing column is always a value of the target/referenced column. Here targets are PKs. Such a join on a FK to one table and a FK to another table will only return rows that share the same PK value, even though the PKs are from different tables.

Regenerate ids of existing records

I have a Rails app with a custom algorithm for id generation for one table. Now I decided to use default incremental ids generated by Postgres as primary keys and move my custom values from id to another column like uid. And I want to regenerate values in id column for all records - as normal from 1 to n.
What is the best way to do this? I have about 1000 records. Records from other tables are associated with these records.
You can keep whatever value is in ID column but create a new column named UID and set it as a primary key and auto increment
def self.up
execute "ALTER TABLE mytable modify COLUMN uid int(8) AUTO_INCREMENT"
end
You can tell your model to use UID as primary key as
self.primary_key = 'uid'
You can simply do it by iterating on your records, and updating them (and their associated objects). 1000 records is not that much to process.
Let's say that you have a table named "my_objects", with its model named "MyObject". Let's also say that you have another table, named "related_objects" and its model "RelatedObject"
You can iterate on all your MyObjects records, and update their related objects and the record itself at the same time.
records = MyObject.all #Whatever "MyObject" you have.
i = 0
records.each do |record|
#Updating whatever associated objects the record have
record.related_objects.each do |related_object|
related_object.update_column("my_object_id", i)
end
#Updating the record itself
record.update_column("id", i)
i++
end

Rails Creating third table from 2 tables

How can I merge column from one table into another table and save it as a variable, what would be the most efficient way of accomplishing this. Here is an example:
Table 1:
ID First Name LastName contact_id
Table 2:
Id Phone Address email
Id from table 2 is a foreign key of contact_id
I need my third table to look like this:
Id Phone Address Email person_id
person_id corresponds to ID from table 1.
What would be the most efficient way using rails commands to accomplish this result so that it gives me the third table?
Why not just add person_id to table 2...
Table1Model.all.each do |model|
x=Table2Model.find(model.contact_id)
x.person_id = model.id
x.save
end

Inserting data as foreign key constraint in grails

Continuing to the post of reading-from-XML-and-storing-the-values-in-database-using-grails
am facing an another problem in it. As the employee id is related to other tables as foreign key how am suppose to insert the data into the database without conflict what i want to add in grails code to avoid the below displayed error.
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Cannot
add or update a child rowL a foreign key constraint fails.. ( CONSTRAINT 'EMPLOYEE_ID_HEADER'
FOREIGN KEY ('EMPLOYEE_ID') REFERENCES 'employee_header'('employee_id'))
here employee_id is a column and employee_header is a separate table that contains employee_id as a foreign key.
Finally I got the answer, when you are inserting the values in database the related foreign key tables will get affects initially. So to handle this situation we have to insert the foreign key related table at first then the table we need to add exactly.
For the reference, at first I had given the code like this
sql.executeInsert("insert into order_item (order_id,product_id,
order_item_seq_id) values (${order_id},${product_id},${order_item_seq_id})")
sql.executeInsert("insert into product(product_id) values(${product_id})")
This first inserts the order_item table then the foreignkey constraint table product, so the data did not inserted. So the correct code is
sql.executeInsert("insert into order_header(order_id) values(${order_id})")
sql.executeInsert("insert into product(product_id) values(${product_id})")
sql.executeInsert("insert into order_item (order_id,product_id,
order_item_seq_id) values (${order_id},${product_id},${order_item_seq_id})")
Now this inserts the data successfully without errors.

Resources