Sunday, September 2, 2012

SQL foreign key issue. Errno 150 in BudgetBuddy

Budget buddy is coming along, Im working on the transactions page. I worked for a couple hours trying to get an htaccess file to work before realizing that my apache server wasn't set up right, and then had to rework a bunch of code to make the site work again. Alot of relative linking was going awry. Luckily a bit of GIT magic brought me back to working correctly so, its ok!

Coding today took a fun detour for about 10 minutes while I tried to figure out why I couldn't apply a foreign key to my database after successfully applying one before. Heres what I had as a problem and how I solved it:

3 Tables, userinfo which has a primary key of userid, accounts which has a userid associated with it and a name (as well as an amount), and then a transactions table I had just made. So I successfully altered transactions to reference userid from userinfo so that it cascades for both updating and deleting. And if anyone doesn't know how to do it, this is the page I go to everytime I need it: This site is good for foreign key info

But then I went to apply the accountname from transactions to be a foreign key from accounts and started getting this errono-150 bit. Completely unhelpful, saying I couldn't create a table such and such. This unhelpful bit led me to google the error number and I quickly stumbled onto this link. After looking at a couple of the reasons for error I figured it was because the account names weren't indexed. I hadn't told them to be primary keys because multiple users could have an account called "Checking". But then I read up on indexing and then used the mysql documentation on create index to add an index to my table. Tried to alter it again, and viola, foreign key accepted.

I hope that if anyone gets an error similar to the one I experienced that they find this blog and use the good couple of links to fix it!

On a side note:
Has anyone ever noticed that viola, when pronounced with a bit of an accent should probably be spelt walah?

1 comment: