Thesql error creating table

MySQL

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!

Best Answer

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)
);