Foreign key creation error in Mariadb

Asked

Viewed 3,038 times

3

When trying to create the toy table, the following error appears:

ERROR 1005 (HY000): Can’t create table brinquedos.brinquedo (Rrno: 150 "Foreign key Constraint is incorrectly Formed")

Follows the code:

CREATE TABLE IF NOT EXISTS categoria (
  categoria_id SERIAL,
  categoria_nome VARCHAR(80) NOT NULL,
  PRIMARY KEY (categoria_id),
  UNIQUE INDEX categoria_nome_UNIQUE (categoria_nome ASC));


CREATE TABLE IF NOT EXISTS brinquedo (
  brinquedo_id SERIAL,
  brinquedo_descricao VARCHAR(180) NULL DEFAULT NULL,
  brinquedo_imagem_url VARCHAR(180) NOT NULL,
  brinquedo_preco DECIMAL(9,2) NOT NULL,
  brinquedo_detalhes VARCHAR(180) NULL DEFAULT NULL,
  brinquedo_categoria_id BIGINT NOT NULL,
  brinquedo_marca VARCHAR(45) NULL DEFAULT NULL,
  PRIMARY KEY (brinquedo_id),
  UNIQUE INDEX brinquedo_imagem_url_UNIQUE (brinquedo_imagem_url ASC),
  CONSTRAINT fk_brinquedo_categoria
    FOREIGN KEY (brinquedo_categoria_id)
    REFERENCES categoria (categoria_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE);

This query worked in mysql, I created months ago in a college job, but now I’m using Mariadb and is giving this error! I don’t know much about the peculiarities of Mysql and Mariadb. In Mysql I used the Innodb default engine. In the Mariadb documentation the default engine is Innodb too, so I’m using Innodb (I think).

From now on I thank those who help me.

  • SERIAL is from Postgresql

  • It even tested what was answered below by brinquedo_categoria_id BIGINT UNSIGNED NOT NULL ?

  • SERIAL works on Mariadb, the first table is successfully created!

  • UNSIGNED, it worked!

1 answer

4


Missing one UNSIGNED in brinquedo_categoria_id since in the original table was defined SERIAL, which also cannot be negative.

  • 2

    Oops! I was slow to answer because I had to do other things here, but your solution worked: brinquedo_categoria_id BIGINT UNSIGNED NOT NULL. That was the problem, thank you! I don’t know why you have a negative vote on your answer!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.