Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:26:44+00:00 2026-06-14T05:26:44+00:00

I have a problem creating a foreign key constraint between two tables. Here is

  • 0

I have a problem creating a foreign key constraint between two tables.

Here is the first table:

CREATE TABLE `agews_rifiuti_cer` (
  `id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `agews_id` int(10) unsigned DEFAULT '0',
  `livello` tinyint(4) DEFAULT '1',
  `codice` varchar(10) DEFAULT NULL,
  `descrizione` varchar(255) DEFAULT NULL,
  `note` text,
  `flag_pericoloso` tinyint(1) DEFAULT '0',
  `id_cliente` int(10) unsigned DEFAULT '1',
  `flag_modificato` char(1) DEFAULT 'N',
  PRIMARY KEY (`id_cer`),
  KEY `fk_id_cliente_agews_sgs_codici_cer` (`id_cliente`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

And here is the second one:

CREATE TABLE `lin_98_47_rifiuti` (
  `id_rifiuto` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_azienda` int(10) unsigned NOT NULL DEFAULT '1',
  `id_sede` int(10) unsigned NOT NULL DEFAULT '1',
  `revisione_documento` int(10) unsigned NOT NULL DEFAULT '0',
  `rifiuto` varchar(255) DEFAULT NULL,
  `codice_cer` varchar(10) DEFAULT NULL,
  `nome_interno` varchar(255) DEFAULT NULL,
  `descrizione` text,
  `materie_prime` text,
  `contenitore` text,
  `deposito` text,
  `data_ultima_analisi` date DEFAULT NULL,
  `stato_fisico` enum('Solido pulverulento','Solido non pulverulento','Fangoso palabile','Liquido') DEFAULT 'Solido non pulverulento',
  `quantita` float(9,1) DEFAULT '0.0',
  `unita_misura` enum('Kg','l','mc') DEFAULT 'Kg',
  `id_pericolo` int(10) unsigned DEFAULT NULL,
  `destino` enum('Recupero','Smaltimento') DEFAULT NULL,
  `id_recupero` int(10) unsigned DEFAULT NULL,
  `id_smaltimento` int(10) unsigned DEFAULT NULL,
  `id_cer` int(10) unsigned DEFAULT NULL,
  `immagine` varchar(255) DEFAULT NULL,
  `image_type` varchar(20) DEFAULT NULL,
  `image_content` mediumblob,
  `image_size_x` smallint(5) unsigned DEFAULT '0',
  `image_size_y` smallint(5) unsigned DEFAULT '0',
  `flag_storico` tinyint(1) DEFAULT '0',
  `id_responsabile` int(10) unsigned DEFAULT '0',
  `nome_responsabile` varchar(255) DEFAULT '0',
  `id_ultima_modifica` int(10) unsigned DEFAULT '0',
  `create_log` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`id_rifiuto`,`id_azienda`,`id_sede`,`revisione_documento`),
  KEY `fk_main_lin_98_47_rifiuti` (`id_azienda`,`id_sede`,`revisione_documento`),
  KEY `fk_id_responsabile_lin_98_47_rifiuti` (`id_responsabile`,`id_azienda`,`id_sede`,`revisione_documento`,`nome_responsabile`),
  KEY `fk_id_pericolo_lin_98_47_rifiuti` (`id_pericolo`),
  KEY `fk_id_recupero_lin_98_47_rifiuti` (`id_recupero`),
  KEY `fk_id_smaltimento_lin_98_47_rifiuti` (`id_smaltimento`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

The problem happens the I try to do this:

ALTER TABLE lin_98_47_rifiuti ADD CONSTRAINT fk_id_cer_lin_98_47_rifiuti FOREIGN KEY (id_cer) REFERENCES agews_rifiuti_cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE;

And I get this error:

#1005 - Can't create table 'db_626suite.#sql-71c_13d5' (errno: 150)

While the command SHOW INNODB STATUS says:

Error in foreign key constraint of table db_626suite/#sql-71c_13d5:
 FOREIGN KEY (id_cer) REFERENCES agews_rifiuti_cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE:
Cannot resolve column name close to:
) ON UPDATE CASCADE ON DELETE CASCADE

But the syntax and fields definitions seem correct to me. What am I doing wrong?

EDIT:

Marc B suggested that it could be due to the fact that id_cer is defined as NOT NULL in agews_rifiuti_cer, but I do not think this is the case, in fact please consider this other table:

CREATE TABLE `agews_rifiuti_pericolo` (
  `id_pericolo` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `agews_id` int(10) unsigned DEFAULT '0',
  `codice` varchar(255) DEFAULT NULL,
  `pericolo` varchar(255) DEFAULT NULL,
  `note` text,
  `id_cliente` int(10) unsigned DEFAULT '1',
  `flag_modificato` char(1) DEFAULT 'N',
  PRIMARY KEY (`id_pericolo`),
  KEY `codice_rifiuti_recupero` (`codice`),
  KEY `fk_id_cliente_rifiuti_pericolo` (`id_cliente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Here id_pericolo is defined as NOT NULL as id_cer above, and yet this works perfectly:

ALTER TABLE lin_98_47_rifiuti ADD CONSTRAINT fk_id_pericolo_lin_98_47_rifiuti FOREIGN KEY (id_pericolo) REFERENCES agews_rifiuti_pericolo(id_pericolo) ON UPDATE CASCADE ON DELETE SET NULL;

even if in lin_98_47_rifiuti the field id_pericolo is defined as DEFAULT NULL

EDIT 2:

I just tried this minimal setup:

CREATE TABLE `cer` (
  `id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `codice` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id_cer`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `rifiuti` (
  `id_rifiuto` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_cer` int(10) unsigned NULL DEFAULT NULL,
  `rifiuto` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id_rifiuto`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

ALTER TABLE rifiuti ADD FOREIGN KEY (id_cer) REFERENCES cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE;

and it all worked correctly, yet I do not understand what the problem might be in my original query.

Also I think this proves that is not a matter of defining the fields as NOT NULL or DEFAULT NULL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:26:46+00:00Added an answer on June 14, 2026 at 5:26 am

    I really do not understand why, but renaming id_cer to id_codice in all tables, while leaving its definition unaltered, has solved the problem.

    So it would seem that MySQL had some kind of problem with that specific field name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problems with creating tables & foreign key. (errno:150) here is a screenshot
I have a problem: (I wouldn't be here otherwise ;) I am creating an
I have a small problem. I am creating an appointment table where in the
I have a script in MySQL that creates two tables, the second table references
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with creating a simple MySQL trigger in C#. I'm using StringBuilder
I have a problem creating a std::map<int, int> from a vector of pointers, called
I have a problem creating the following SQL Statement using LINQ & C# select
I have a problem creating and executing a JAR file. I have already made
I have encountered a problem with creating a thumbnail from an uploaded image file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.