Skip to main content

Insert into a foreign key field looks like it should match.....

You bring data into a referenced column through LOAD DATA LOCAL INFILE but when you try to insert data into the referencing column you get the "Cannot add or update a foreign key constraint fails...." error. You have looked at the referenced column and the referencing data again and again and you don't see the problem.

If you open up Query Browser and look at the referenced data you may find paragraph marks. This problem confounded me for quite some time. I understood that the paragraph marks were causing the problem but I didn't understand how they were getting there.

This is a classic case where "RTFM" applies. MySQL by default understands line termination by the return character "\r". The Windows OS by default writes line termination as a carriage return and a new line "\r\n". The extra \n is causing the problem.

How to deal with this is right in the MySQL manual. But I thought I would post it because I do and did "RTFM" and still missed it. So maybe I can save someone else the frustration. The solution is to simply use LINES TERMINATED BY... at the end of your load data statement like so

LOAD DATA LOCAL INFILE "/path/to/file" INTO TABLE tablename LINES TERMINATED BY '\r\n'.

This can also cause you trouble in reverse if you are importing referencing data with the wrong kind of line termination

Comments

Popular posts from this blog

Running Goals or Running Goal; What is it That I Want?

The other day I got all caught up in setting running goals for myself for the rest of the year. After the goals were set, I found myself starting to worry about how I was going to reach them. Wondering if there would be time to sufficiently prepare for them all. My goals were to run a 5k in under 20 minutes, to run a half marathon in 1:25 minutes and qualify for and run in the Boston marathon in 2014. At first, I thought that setting and meeting these goals would make me a more serious runner. In fact, I thought that to be the "serious" runner I want to be that I needed these goals. However, while I was running the other morning I realized that first of all, I was thinking about how to shift my training to meet my 5k goal. Then I began thinking about the right approach to a 1:25 half while training to qualify for Boston in a full marathon a month later. That is when I realized that the ancillary goals were starting to consume my focus and quite possibly i...

The Rosary: Staying Focused

There are several obstacles that someone who has committed to pray the Rosary will face. Since the Rosary is a long prayer one of the biggest challenges I face is staying focused. If you have read my other posts or if you are just familiar with the prayer, you know that the mental contemplation of the Mysteries is the main focus of the prayer. So getting distracted really detracts from the prayer. Even if I am only praying 1 chaplet of the Rosary, it still takes about 20 minutes. In that amount of time your mind is bound to wander some. I have found several different things to help me when my mind begins to drift off to the worries of everyday life. The first is to understand that this is going to happen and be watching for it. If I am not vigilant I can get through a whole decade and realize I have not thought one single time about the Mystery that I should have been contemplating. So before I begin the prayer I make a conscious determination to be watchful of my wandering mind. O...

2019 Indianapolis Monumental Marathon Race Report

On November 9th, 2019 I ran the Indianapolis Monumental Marathon (IMM). The short report is that I whined a lot during the build up and incessantly complained that I had gotten fat and slow but on race day, a semblance of my old self showed up and ran 1 hell of a race crushing my stated goal of 2:57 by nearly 4 minutes. The long report follows. I really had kind of given up on ever running a fast marathon again. After my back issues last year I just never got going full steam again. Instead I had gotten fat and slow because I let my diet slide and put on about 15 pounds and I was trying to run without fully committing to the work of marathon training. This summer I had decided to just maybe, kind of run the IMM for fun and not really lose my summer to training for it. But then Chad, a friend of mine who runs with a group on Sundays that I sometimes go to, asked my if I wanted to do my long runs for IMM together. I reluctantly agreed. Chad and I put up a couple of solid long...