Record deletion Error "unknown column id in where clause" ActiveJDBC - activejdbc

When I delete a record it shows an weird error,
"unknown column 'id' in 'where clause'. DELETE FROM tableName WHERE id='' null"
Something like that. The error seems to be self explanatory, BUT in my table I dont have a column id.
Table Structure:
Code
Name
UserGroup
Why is ActiveJDBC searching an column id?
The error disappears when I added column id in my table. But still its weird.

Because this is a requirement of the framework. Did you read the documentation: http://javalite.io/surrogate_primary_keys ?
If you want to delete the parent, and then children with it, there are many ways to do this. One is to use the framework:
http://javalite.io/delete_cascade
The other is to use a DB mechanism: MySQL delete on cascade
Additionally, please really study relationships and read the docs before imoplementing: http://javalite.io/one_to_many_associations
Good luck!

Related

The INSERT statement conflicted with the FOREIGN KEY constraint__ [duplicate]

I am getting the following error. Could you please help me?
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Sup_Item_Sup_Item_Cat". The conflict occurred in database "dev_bo", table "dbo.Sup_Item_Cat". The statement has been terminated.
Code:
insert into sup_item (supplier_id, sup_item_id, name, sup_item_cat_id,
status_code, last_modified_user_id, last_modified_timestamp, client_id)
values (10162425, 10, 'jaiso', '123123',
'a', '12', '2010-12-12', '1062425')
The last column client_id is causing the error. I tried to put the value which already exists in the dbo.Sup_Item_Cat into the column, corresponding to the sup_item.. but no joy :-(
In your table dbo.Sup_Item_Cat, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
If you have SQL Server Management Studio, open it up and sp_help 'dbo.Sup_Item_Cat'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.
Let me know if you need anything explained better!
I had this issue myself, regarding the error message that is received trying to populate a foreign key field. I ended up on this page in hopes of finding the answer. The checked answer on this page is indeed the correct one, unfortunately I feel that the answer is a bit incomplete for people not as familiar with SQL. I am fairly apt at writing code but SQL queries are new to me as well as building database tables.
Despite the checked answer being correct:
Mike M wrote-
"The way a FK works is it cannot have a value in that column that is
not also in the primary key column of the referenced table."
What is missing from this answer is simply;
You must build the table containing the Primary Key first.
Another way to say it is;
You must Insert Data into the parent table, containing the Primary
Key, before attempting to insert data into the child table containing
the Foreign Key.
In short, many of the tutorials seem to be glazing over this fact so that if you were to try on your own and didn't realize there was an order of operations, then you would get this error. Naturally after adding the primary key data, your foreign key data in the child table must conform to the primary key field in the parent table, otherwise, you will still get this error.
If anyone read down this far. I hope this helped make the checked answer more clear. I know there are some of you who may feel that this sort of thing is pretty straight-forward and that opening a book would have answered this question before it was posted, but the truth is that not everyone learns in the same way.
You are trying to insert a record with a value in the foreign key column that doesn't exist in the foreign table.
For example: If you have Books and Authors tables where Books has a foreign key constraint on the Authors table and you try to insert a book record for which there is no author record.
You'll need to post your statement for more clarification. But...
That error means that the table you are inserting data into has a foreign key relationship with another table. Before data can be inserted, the value in the foreign key field must exist in the other table first.
The problem is not with client_id from what I can see. It looks more like the problem is with the 4th column, sup_item_cat_id
I would run
sp_helpconstraint sup_item
and pay attention to the constraint_keys column returned for the foreign key FK_Sup_Item_Sup_Item_Cat to confirm which column is the actual problem, but I am pretty sure it is not the one you are trying to fix. Besides '123123' looks suspect as well.
Something I found was that all the fields have to match EXACTLY.
For example, sending 'cat dog' is not the same as sending 'catdog'.
What I did to troubleshoot this was to script out the FK code from the table I was inserting data into, take note of the "Foreign Key" that had the constraints (in my case there were 2) and make sure those 2 fields values matched EXACTLY as they were in the table that was throwing the FK Constraint error.
Once I fixed the 2 fields giving my problems, life was good!
If you need a better explanation, let me know.
I ran into this problem when my insert value fields contained tabs and spaces that were not obvious to the naked eye. I had created my value list in Excel, copied, and pasted it to SQL, and run queries to find non-matches on my FK fields.
The match queries did not detect there were tabs and spaces in my FK field, but the INSERT did recognize them and it continued to generate the error.
I tested again by copying the content of the FK field in one record and pasting it into the insert query. When that record also failed, I looked closer at the data and finally detected the tabs/spaces.
Once I cleaned removed tabs/spaces, my issue was resolved. Hope this helps someone!
Double check the fields in the relationship the foreign key is defined for. SQL Server Management Studio may not have had the fields you wanted selected when you defined the relationship. This has burned me in the past.
run sp_helpconstraint
pay ATTENTION to the constraint_keys column returned for the foreign key
I had the same problem when I used code-first migrations to build my database for an MVC 5 application. I eventually found the seed method in my configuration.cs file to be causing the issue. My seed method was creating a table entry for the table containing the foreign key before creating the entry with the matching primary key.
Parent table data missing causes the problem.
In your problem non availability of data in "dbo.Sup_Item_Cat" causes the problem
I also got the same error in my SQL Code, This solution works for me,
Check the data in Primary Table May be you are entering a column value which is not present in the primary key column.
The problem was reproducible and intermittent for me using mybatis.
I'm sure I had correct DB configuration (PK, FK, auto increment etc)
I'm sure I had correct order of insertions (parent records first), in debug I could see parent record inserted with respective PK and just after that next statement failed with inserting child record with correct FK inside.
The problem was fixed by for reseeding identity with
DBCC CHECKIDENT ('schema.customer', RESEED, 0);
DBCC CHECKIDENT ('schema.account', RESEED, 0);
Exactly the same code that failed before started to work.
I would like somebody to explain me what was causing the issue.
In my case, I was inserting the values into the child table in the wrong order:
For the table with 2 columns: column1 and column2, I got this error when I mistakenly entered:
INSERT INTO Table VALUES('column2_value', 'column1_value');
The error was resolved when I used the below format:-
INSERT INTO Table (column2, column1) VALUES('column2_value', 'column1_value');
If your FK column table should contain that FK value as a primary key Value then data will be inserted.

You cannot add or update record (MS Access)

Can someone help me? I am really new to access and vb . net. Every time i try to enforce referential integrity in my database relationship and add records or update a record I a always get a message of "You cannot add or update record because a related record is required in the table tbProducts"
Here is my database relationship.Database Relationship
The problem would likely be that a Foreign Key reference is required in your tbProducts table, the only one I can see from your image is UserID. (from the tbStaff table)
If you're trying to insert a record into the tbProducts, make sure that there is an existing UserID value to match what your inserting.

Two entities with possibly different keys are mapped to the same row

I have Table
Field1 PK int not null
Field2 PK int not null like this
when i want to map this table I get this error
Error 3 Error 3034: Problem in mapping fragments starting at lines 2212, 2218:
Two entities with possibly different keys are mapped to the same row.
Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.
And I tried to delete and re-create that table inside model. When i add table it gives another error but at the end i always get this error
How can i handle this problem please help me...
When i investigate the problem, i realised something becuse i read an article before for this data model situation and i understand the why the problem occur. (Problem in mapping fragments in Entity Framework)
Actually problem comes from table mapping because i said that table has many to many relationship so That article says if you put that table in the model design it always gives us this error and finally when we delete that table on design side and add table silently in data model so program can be build. I ‘m telling you this because maybe you can make an idea for this because i learned this and when i add new view in model and vs doesnt give me any error.
This is the old model picture when the delete that table inside red box The program doesn't give any error and continiue to use bottom table
If nothing else works, try deleting and recreating the whole .edmx-file.
I got this problem when I added a couple of many-to-many tables to an existing Entity Framework 6.2 project.
I tried removing and regenerating all tables in the model. Nothing seemed to work. I believe there was some "junk" somewhere in the EF project causing the problem.
When regenerating the edmx, I added all tables in the project, including the many-to-many tables.
solution for error:3034
PK UserId;
PK RoleId
two primary key cannot allow edmx file.so create sno column in that table and make that as primary key . remove the pk of UserId and RoleId.
now in that two column there is no primary key available.
like
Pk sno;
FK UserId;
FK RoleId

How to update same columns on different tables at the same time?

I have Table-A with a column called serial_number, and Table-B has the same column.
Update a single table is not possible to their linkage, upon attempt though, this is the error:
The UPDATE statement conflicted with the FOREIGN KEY constraint
"L_231". The conflict occurred in
database "Main", table "dbo.Products".
The statement has been terminated.
I know how I can delete the field in both in case I need to, I just don't know how to update it simultaneously.
Thanks.
this is a BAD design. if this isn't a "one time thing" to fix an issue, but part of your applicaion, you need a redesign.
You shouldn't update keys, use a surrogate key (like an identity) instead and then store the serial_number as a plain data column (which can be easily changed).
You should be able to set the constraint as ON UPDATE CASCADE.
Hello
Good question but ????
one Method is Update trigger
you update in same time on different table

INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server

I am getting the following error. Could you please help me?
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Sup_Item_Sup_Item_Cat". The conflict occurred in database "dev_bo", table "dbo.Sup_Item_Cat". The statement has been terminated.
Code:
insert into sup_item (supplier_id, sup_item_id, name, sup_item_cat_id,
status_code, last_modified_user_id, last_modified_timestamp, client_id)
values (10162425, 10, 'jaiso', '123123',
'a', '12', '2010-12-12', '1062425')
The last column client_id is causing the error. I tried to put the value which already exists in the dbo.Sup_Item_Cat into the column, corresponding to the sup_item.. but no joy :-(
In your table dbo.Sup_Item_Cat, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
If you have SQL Server Management Studio, open it up and sp_help 'dbo.Sup_Item_Cat'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.
Let me know if you need anything explained better!
I had this issue myself, regarding the error message that is received trying to populate a foreign key field. I ended up on this page in hopes of finding the answer. The checked answer on this page is indeed the correct one, unfortunately I feel that the answer is a bit incomplete for people not as familiar with SQL. I am fairly apt at writing code but SQL queries are new to me as well as building database tables.
Despite the checked answer being correct:
Mike M wrote-
"The way a FK works is it cannot have a value in that column that is
not also in the primary key column of the referenced table."
What is missing from this answer is simply;
You must build the table containing the Primary Key first.
Another way to say it is;
You must Insert Data into the parent table, containing the Primary
Key, before attempting to insert data into the child table containing
the Foreign Key.
In short, many of the tutorials seem to be glazing over this fact so that if you were to try on your own and didn't realize there was an order of operations, then you would get this error. Naturally after adding the primary key data, your foreign key data in the child table must conform to the primary key field in the parent table, otherwise, you will still get this error.
If anyone read down this far. I hope this helped make the checked answer more clear. I know there are some of you who may feel that this sort of thing is pretty straight-forward and that opening a book would have answered this question before it was posted, but the truth is that not everyone learns in the same way.
You are trying to insert a record with a value in the foreign key column that doesn't exist in the foreign table.
For example: If you have Books and Authors tables where Books has a foreign key constraint on the Authors table and you try to insert a book record for which there is no author record.
You'll need to post your statement for more clarification. But...
That error means that the table you are inserting data into has a foreign key relationship with another table. Before data can be inserted, the value in the foreign key field must exist in the other table first.
The problem is not with client_id from what I can see. It looks more like the problem is with the 4th column, sup_item_cat_id
I would run
sp_helpconstraint sup_item
and pay attention to the constraint_keys column returned for the foreign key FK_Sup_Item_Sup_Item_Cat to confirm which column is the actual problem, but I am pretty sure it is not the one you are trying to fix. Besides '123123' looks suspect as well.
Something I found was that all the fields have to match EXACTLY.
For example, sending 'cat dog' is not the same as sending 'catdog'.
What I did to troubleshoot this was to script out the FK code from the table I was inserting data into, take note of the "Foreign Key" that had the constraints (in my case there were 2) and make sure those 2 fields values matched EXACTLY as they were in the table that was throwing the FK Constraint error.
Once I fixed the 2 fields giving my problems, life was good!
If you need a better explanation, let me know.
I ran into this problem when my insert value fields contained tabs and spaces that were not obvious to the naked eye. I had created my value list in Excel, copied, and pasted it to SQL, and run queries to find non-matches on my FK fields.
The match queries did not detect there were tabs and spaces in my FK field, but the INSERT did recognize them and it continued to generate the error.
I tested again by copying the content of the FK field in one record and pasting it into the insert query. When that record also failed, I looked closer at the data and finally detected the tabs/spaces.
Once I cleaned removed tabs/spaces, my issue was resolved. Hope this helps someone!
Double check the fields in the relationship the foreign key is defined for. SQL Server Management Studio may not have had the fields you wanted selected when you defined the relationship. This has burned me in the past.
run sp_helpconstraint
pay ATTENTION to the constraint_keys column returned for the foreign key
I had the same problem when I used code-first migrations to build my database for an MVC 5 application. I eventually found the seed method in my configuration.cs file to be causing the issue. My seed method was creating a table entry for the table containing the foreign key before creating the entry with the matching primary key.
Parent table data missing causes the problem.
In your problem non availability of data in "dbo.Sup_Item_Cat" causes the problem
I also got the same error in my SQL Code, This solution works for me,
Check the data in Primary Table May be you are entering a column value which is not present in the primary key column.
The problem was reproducible and intermittent for me using mybatis.
I'm sure I had correct DB configuration (PK, FK, auto increment etc)
I'm sure I had correct order of insertions (parent records first), in debug I could see parent record inserted with respective PK and just after that next statement failed with inserting child record with correct FK inside.
The problem was fixed by for reseeding identity with
DBCC CHECKIDENT ('schema.customer', RESEED, 0);
DBCC CHECKIDENT ('schema.account', RESEED, 0);
Exactly the same code that failed before started to work.
I would like somebody to explain me what was causing the issue.
In my case, I was inserting the values into the child table in the wrong order:
For the table with 2 columns: column1 and column2, I got this error when I mistakenly entered:
INSERT INTO Table VALUES('column2_value', 'column1_value');
The error was resolved when I used the below format:-
INSERT INTO Table (column2, column1) VALUES('column2_value', 'column1_value');
If your FK column table should contain that FK value as a primary key Value then data will be inserted.