The system is starting to complain about the number of comments.
I'd be interested in seeing the statements as they are sent to the server - i.e. the fully generated SQL statements, i.e. with real values, not ? parameter place holders.
The statement that appears to be causing the issue is this one.
INSERT INTO `inventoryitems` VALUES (DEFAULT, 4, '595', null, 5390001, 5, 1, 1, '', -1, 0, -1, '')
Now, inventory items has a FOREIGN KEY constraint
CONSTRAINT FK_inventoryitems_1
FOREIGN KEY (characterid
) REFERENCES characters
(id
) ON DELETE CASCADE
What is the result of the following query
SELECT COUNT(*) FROM characters WHERE characters.id = 595;
The values here are also of interest.
ps = con.prepareStatement("INSERT INTO `inventoryitems` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
pse = con.prepareStatement("INSERT INTO `inventoryequipment` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
[EDIT]
I'm starting to get some insight into what may be going on. You appear to be using some sort of framework for your database access (that's the reason why your SQL has '595' for an integer insert instead of 595.
It's possible that the transaction isolation level is being set by the framework to something other than read committed (see here). Try and ensure that your transactions use read committed - set the transaction level. Try adding this code to yours after the line
Connection con = DatabaseConnection.getConnection();
con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); // Added line.
Also, check out this and the rest of the thread.
[EDIT] in response to "How do i check it? , can you guide me?"
What I meant was for you to add the line above to your code and rerun your system to the point where you got your original error. Do you still get an error? If you do, then is it different to the original error? If it is different, then post the error text here. HTH.
Could you take a dump of your database and put it online? You can anonymise anything sensitive. Also, zip up your Java code and I'll run that here against your schema. My Java's a bit rusty, but running it on a different system might give us some clues! :-)
[EDIT] in response to OP's comment "can you give me an example of indexes [code example] ?".
See here (you can also use the KEY keyword as a synonym for INDEX - see the docco here. You should add to your inventoryitems CREATE TABLE statement lines such as
INDEX `ii_type_ix` (type),
INDEX `ii_account_id_ix` (accountid),
As for changing your Java code - I'm not 100% sure, but the problem lies with the database so your efforts should be concentrated there. Where you can change the code is in varying the order of your queries (but keeping the original functionality).
Best Answer
As i said in my comment, use Triggers for that purpose
Like shown here, you have add every constraint as if clause to your trigger., because mysql 5.x doesn't support
CHECK
constraintsAlso note that
Order
is a reserved word, and you shouldn't use them in table or column names else you have always to use Backticks in every QueryYour field
Engine_Description
is declared as NOT NULL so it can't be set to NULL i set it so to an empty descritiondb<>fiddle here