I am having an issue with inputting data from a csv file into a mysql database using JDBC. I have already inserted a table called 'Poet' with the 'PoetName' being the priamry key.
I have populated that table with records that i have held in a CSV file, I then created another table called 'Poem' and it contains a foreign key which is 'PoetName' which references the poet table however whenever I try to populate the table with the CSV file for poems that contains matching values I am being displayed with the following error-
"Cannot add or update a child row: a foreign key constraint fails"
This is strange because the values I have for the primary key 'PoetName' are the same for the values I have used for the foreign key.
Does anyone have an idea of what the issue may be?
Thanks
If you're running into this error, one quick workaround is to add this line to your script:
SET FOREIGN_KEY_CHECKS = 0;
Another note is generally you want your primary key to be of type INT.
Related
I'm trying to make a User-Bundle relation, where the relation between them is many-many.
So, to break this many to many into 2 one-to-many, I've created another table called subscription.
The subscription table has primary keys of both user and bundle as foreign keys. The Subscription table has a composite key made of (userId,bundleId), where both these columns are not null in their respective table.
Now, when I try to insert data in the subscription table using mybatis, it gives error userId cannot be null.
The reason being, mybatis sql is converted to a prepared statement which is used for dynamic parameters.
Here's an insert statement to give you a rough idea about my subscription table
INSERT INTO Subscription (USERID,BUNDLEID,BUNDLESTARTDATE)
VALUES (#{userId},#{bundleId},#{promotionStartDate});
Although I'm passing the userId, it doesn't accept it and calls userId cannot be null. I'm fairly sure this is due to the prepared statement. Please help.
I have a issue when generating code using Telosys.
After configuring all the information, when I click on Create model
I get the following issue:
If I remove foreign key from table and click Create model then this works fine.
It has become like have to remove the foreign key from table and Create model and then add back the foreign key again.
How to do this without removing foreign key constraint?
PS: I just use this tool to create Spring JPA entity.
After looking in the source code it seems that the table name referenced by the FK is not found in the model.
May be due to upper case / lower case difference in the table name. For example a FK referencing the "Foo" table instead of "FOO" (or vice versa).
You can check the table names retrieved from the database with "Get tables" in the "Meta-data" tab. Use "Get foreign keys" to check the Foreign Keys :
I guess you skipped configuration. You can customize existing templates in order to generate jpa DAO
the templates available on GitHub : https://github.com/telosys-templates-v3
I'm working on a servlet that needs to insert some data to the db table with a composite primary key consists of the userid, dataid and CURRENT_TIMESTAMP.
however im getting the following error when executing the query
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry'13-7-2013-09-13 23:22:24' for key 'PRIMARY'
I think this is caused by the multiple insertion of rows to the same table in the same time, though with different dataid. Is there any solution to this problem? Should I cancel CURRENT_TIMESTAMP as a part of the primary key to do the trick or there are some other better workarounds?
Thanks a lot and appreciate for any help!
If it's a log table, it is recommended not to use a primary key. If you want to accelerate some search in this table, create the appropriate indexes.
If you need a primary key (for example, if you plan to use it with JPA), it would be best to use a number, e.g.
ID int AUTO_INCREMENT PRIMARY KEY
For example, log4j can insert each event log into a database using a org.apache.log4j.jdbc.JDBCAppender.
See also MySQL storage engine for a large log table.
I currently have a schema set up with my database and Java Application using OpenJPA that works most of the time, but sometimes I get the error for a few users:
org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "table1.ID" to two different values
table1 actually has a composite Key (two values) and each value in that key is a foreign key to another table. I used RSA (Rational Software Architect) to set up the entities for me (generated code). It set up a PK class (using #EmbeddableId to reference the PK class) in the Entity class for table1, and then two #ManyToOne relationships in the same table1 Entity class (and also in the entity classes that those columns reference) since they are foreign keys
Now, as I mentioned above, each value in the composite key is a foreign key. Well, each of those foreign keys is actually generated using an outside Sequencer in their own entity classes. I am using DB2 and using #GeneratedValue on the columns (i.e. the IDs in table2's and table3's entity classes). I use strategy=GenerationType.SEQUENCE also for each.
Again, everything works USUALLY but not 100% of the time and I'm unsure why. I have gotten rid of this error by wiping out everything and resetting the Sequence Generators, but I know this is definitely not a solution. Could it have something to do with the fact that the two Composite Key values in the database are foreign keys to columns which were generated using a sequence, but the PK entity might not know?
I have noticed too that it only works for users who have a record in the Users table (one of the foreign keys mentioned above is to a Users table, while the other FK is to another table). What happens, if a user is not in the table, it creates one, something like:
User newUser = userManager.getNewUser();
newUser.setName(..);
newUser.setEmail(..);
...
When it's done, the PK class I mentioned above has a new instance of that created, which is then called into another table. The ID from the user above is passed into the PK. Like:
PK newPK = pkManager.getNewPK();
newPk.setAID(newUser.getID());
Has anybody run into this? Any solutions?
Sorry, fixed the problem. I went through my code and realized I had forgot to refactor one line of code (change in data model).
Simple question. Just wondering if this can be done without me having to enforce this constraint manually in my Java code. These two foreign keys (together in the same table) both refer out to another table, but for each row, they must not be allowed to point to the same foreign item.
link text
You can use a check contraint to enforce that two columns have different values:
ALTER TABLE TableName
ADD CONSTRAINT ConstraintName
CHECK fk1 <> fk2