Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I was creating a DB with mySQL, and this error appeared:

Can't create table 'projectefinal.apuntats' (errno: 150)

I've got this:

    CREATE DATABASE IF NOT EXISTS projectefinal

CREATE TABLE IF NOT EXISTS usuaris(
    id INT NOT NULL AUTO_INCREMENT,
    nom TEXT NOT NULL,
    cognoms TEXT NOT NULL,
    dni VARCHAR(9) NOT NULL,
    aturat BOOLEAN NOT NULL,
    PRIMARY KEY(id),
    UNIQUE(dni)
);

CREATE TABLE IF NOT EXISTS cursos(
    id INT NOT NULL AUTO_INCREMENT,
    nom TEXT NOT NULL,
    maximspersones INT NOT NULL,
    PRIMARY KEY(id)
);

CREATE TABLE IF NOT EXISTS apuntats(
    id INT NOT NULL AUTO_INCREMENT,
    idcurs INT NOT NULL,
    idusuari INT NOT NULL,
    coneixements TEXT,
    motiu TEXT,
    FOREIGN KEY(idcurs)
        REFERENCES id(curs)
        ON DELETE CASCADE,
    FOREIGN KEY(idusuari)
        REFERENCES id(usuaris)
        ON DELETE CASCADE,
    PRIMARY KEY(id)
);

I've read before that may happends because a table is not created, but it is, I went one by one ("usuaris" first, "curs" for the second, and finally "apuntats") and it gave me that error. Any help on how to fix it?

Thanks again, as always!

share|improve this question

migrated from stackoverflow.com Apr 13 '13 at 0:26

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 3 down vote accepted

your foreign key definitions are wrong, you switched table and column names.

this should work:

CREATE TABLE IF NOT EXISTS apuntats(
    id INT NOT NULL AUTO_INCREMENT,
    idcurs INT NOT NULL,
    idusuari INT NOT NULL,
    coneixements TEXT,
    motiu TEXT,
    FOREIGN KEY(idcurs)
        REFERENCES cursos(id)
        ON DELETE CASCADE,
    FOREIGN KEY(idusuari)
        REFERENCES usuaris(id)
        ON DELETE CASCADE,
    PRIMARY KEY(id)
);
share|improve this answer
    
ooh, my god xD this things I never saw them! thanks! –  Ardilla Apr 13 '13 at 0:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.