#1822 - Failed to add the foreign key constraint [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
MySQL gives me this error:
Error Code: #1822 - Failed to add the foreign key constraint. Missing index for constraint '' in the referenced table 'produtos'
This is my database:
-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: 10-Jun-2018 às 22:31
-- Versão do servidor: 5.6.39
-- PHP Version: 5.6.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
--
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `fornecedores`
--
CREATE TABLE `fornecedores` (
`Id_Forn` int(9) NOT NULL,
`Nome` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Empresa` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Descrição` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`Referencia` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Estrutura da tabela `funcionários`
--
CREATE TABLE `funcionários` (
`ID_Func` int(9) NOT NULL,
`First_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Last_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`User` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`PassWord` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`NIF` int(9) NOT NULL,
`Morada` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`Vencimento` int(9) NOT NULL,
`Cargo` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPACT;
--
-- Extraindo dados da tabela `funcionários`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `newsletter`
--
CREATE TABLE `newsletter` (
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Estrutura da tabela `produtos`
--
CREATE TABLE `produtos` (
`Referencia` int(20) NOT NULL,
`Tipo` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`Nome` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`Descricao` varchar(10000) COLLATE utf8_unicode_ci NOT NULL,
`Imagens` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`Stock` int(3) NOT NULL,
`Numero_Serie` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`Preco` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Extraindo dados da tabela `produtos`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `utilizadores`
--
CREATE TABLE `utilizadores` (
`ID_Uti` int(9) NOT NULL,
`First_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Las_name` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`PassWord` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Newsletter` int(1) DEFAULT NULL,
`Morada` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`Codigo_Postal` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
`Conselho` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`NIF` int(9) DEFAULT NULL,
`Ativo` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Extraindo dados da tabela `utilizadores`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `vendas`
--
CREATE TABLE `vendas` (
`ID_Vendas` int(9) NOT NULL,
`ID_Uti` int(9) NOT NULL,
`Referencia` int(20) NOT NULL,
`Data` date NOT NULL,
`Estado` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `fornecedores`
--
ALTER TABLE `fornecedores`
ADD PRIMARY KEY (`Id_Forn`),
ADD UNIQUE KEY `Contacto` (`Contacto`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`) ;
--
-- Indexes for table `funcionários`
--
ALTER TABLE `funcionários`
ADD PRIMARY KEY (`ID_Func`),
ADD UNIQUE KEY `Email` (`Email`),
ADD UNIQUE KEY `Contacto` (`Contacto`);
--
-- Indexes for table `newsletter`
--
ALTER TABLE `newsletter`
ADD PRIMARY KEY (`Email`);
--
-- Indexes for table `produtos`
--
ALTER TABLE `produtos`
ADD PRIMARY KEY (`Referencia`),
ADD UNIQUE KEY `Nome` (`Nome`);
--
-- Indexes for table `utilizadores`
--
ALTER TABLE `utilizadores`
ADD PRIMARY KEY (`ID_Uti`),
ADD UNIQUE KEY `Email` (`Email`);
--
-- Indexes for table `vendas`
--
ALTER TABLE `vendas`
ADD PRIMARY KEY (`ID_Vendas`),
ADD UNIQUE KEY `ID_Uti` (`ID_Uti`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`ID_Uti`) REFERENCES `utilizadores` (`ID_Uti`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `fornecedores`
--
ALTER TABLE `fornecedores`
MODIFY `Id_Forn` int(9) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `funcionários`
--
ALTER TABLE `funcionários`
MODIFY `ID_Func` int(9) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15582;
--
-- AUTO_INCREMENT for table `produtos`
--
ALTER TABLE `produtos`
MODIFY `Referencia` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=96455;
--
-- AUTO_INCREMENT for table `vendas`
--
ALTER TABLE `vendas`
MODIFY `ID_Vendas` int(9) NOT NULL AUTO_INCREMENT;
COMMIT;
I can't understand where the error is.

Foreign keys need to point to a primary key or unique constraint. However, the script you are using tries to create a foreign key when the referenced column is not (yet) a primary key.
See the error below?
ALTER TABLE `fornecedores`
ADD PRIMARY KEY (`Id_Forn`),
ADD UNIQUE KEY `Contacto` (`Contacto`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`) ;
At this point the column Referencia is not yet a primary key on produtos. It's made a primary key a few lines later in your script.
Solution? Just rearrange your script to create all primary keys first, and foreign keys second. Easy.

Related

mysql update parent updates all column values in child

I have the following tables:
-users(not important here)
-students
-courses
-enrollments
the students and courses table have a "active column"
the enrollments table have a "course_active" and "student_active" column.
I am trying to set my DB in such a way that when I change active for student or course this will cascade to enrollments.
Unfortunately I am seeing that when I update an active value on "students/courses"
the whole column of "student active" or "course active" changes.
I would truly appreciate your input and assistance on this matter.
link to a fiddle for convenience:
http://sqlfiddle.com/#!9/8e2ce
here is my SQL:
-- phpMyAdmin SQL Dump
-- version 4.8.0.1
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Apr 20, 2018 at 08:32 AM
-- Server version: 10.1.28-MariaDB
-- PHP Version: 7.1.10
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `schoolcrm2`
--
-- --------------------------------------------------------
--
-- Table structure for table `courses`
--
CREATE TABLE `courses` (
`id` int(5) NOT NULL,
`name` varchar(50) NOT NULL,
`description` text NOT NULL,
`start_date` date NOT NULL,
`end_date` date NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`active` tinyint(1) NOT NULL DEFAULT '1',
`added_by` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `enrollments`
--
CREATE TABLE `enrollments` (
`id` int(5) NOT NULL,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`student_active` int(1) NOT NULL DEFAULT '1',
`course_active` int(1) NOT NULL DEFAULT '1',
`added_by` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `students`
--
CREATE TABLE `students` (
`id` int(5) NOT NULL,
`email` varchar(40) NOT NULL,
`name` varchar(30) NOT NULL,
`phone` varchar(30) NOT NULL,
`password` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`active` tinyint(1) NOT NULL DEFAULT '1',
`added_by` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(5) NOT NULL,
`email` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`phone` varchar(30) NOT NULL,
`password` varchar(255) NOT NULL,
`role` enum('1','2','3') DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`active` tinyint(1) NOT NULL DEFAULT '1',
`added_by` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `courses`
--
ALTER TABLE `courses`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `enrollments`
--
ALTER TABLE `enrollments`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `students`
--
ALTER TABLE `students`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `email` (`email`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `email` (`email`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `courses`
--
ALTER TABLE `courses`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `enrollments`
--
ALTER TABLE `enrollments`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `students`
--
ALTER TABLE `students`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
ALTER TABLE `enrollments`
ADD CONSTRAINT `enrollments_to_courses` FOREIGN KEY (`course_active`) REFERENCES `courses` (`is_active`) ON UPDATE CASCADE,
ADD CONSTRAINT `enrollments_to_students` FOREIGN KEY (`student_active`) REFERENCES `students` (`is_active`) ON UPDATE CASCADE;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;

Some foreign key doesn t work

I am trying to create a data base withe relation ship. So with PHPMYADMIN, I use the conceptor mode only for my relationship. So I see that my differents table are linked. But when I export my data base, I see that relationships are broken except two or three. I dont know why. I tryed to with sql code but its the same.
relationship who works :
after export
that I want :
before export.
In this picture it is all the relationships that I want.
but if I do this code
ALTER TABLE `personnel`
ADD CONSTRAINT `personnel_ibfk_1` FOREIGN KEY (`idDroit`) REFERENCES `droit` (`id`);
I have no error but in the conceptor mode there is no link between tables.
here my sql code : (it s a french table , sorry ...)
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Client : 127.0.0.1
-- Généré le : Sam 07 Octobre 2017 à 10:14
-- Version du serveur : 5.6.17
-- Version de PHP : 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Base de données : `projdb`
--
-- --------------------------------------------------------
--
-- Structure de la table `camps`
--
CREATE TABLE IF NOT EXISTS `camps` (
`idinfrastructure` int(11) NOT NULL,
`capacite` int(11) NOT NULL,
UNIQUE KEY `idinfrastructure` (`idinfrastructure`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `centrale`
--
CREATE TABLE IF NOT EXISTS `centrale` (
`idinfrastructure` int(11) NOT NULL,
UNIQUE KEY `idinfrastructure` (`idinfrastructure`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `commande`
--
CREATE TABLE IF NOT EXISTS `commande` (
`id` int(11) NOT NULL,
`idCentrale` int(11) NOT NULL,
`date` datetime NOT NULL,
`etat` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `commande_produit`
--
CREATE TABLE IF NOT EXISTS `commande_produit` (
`idCommande` int(11) NOT NULL,
`idProduit` int(11) NOT NULL,
`quantite` int(11) NOT NULL,
KEY `idCommande` (`idCommande`),
KEY `idProduit` (`idProduit`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `droit`
--
CREATE TABLE IF NOT EXISTS `droit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`niveauPrivillege` int(11) NOT NULL,
`nom` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `niveauPrivillege` (`niveauPrivillege`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Structure de la table `infrastructure`
--
CREATE TABLE IF NOT EXISTS `infrastructure` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(100) NOT NULL,
`latitude` varchar(100) NOT NULL,
`longitude` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Structure de la table `personnel`
--
CREATE TABLE IF NOT EXISTS `personnel` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idDroit` int(11) NOT NULL,
`nom` varchar(50) NOT NULL,
`prenom` varchar(50) NOT NULL,
`date_nais` date NOT NULL,
`adresse` varchar(300) NOT NULL,
`telephone` varchar(35) CHARACTER SET utf32 NOT NULL,
`mail` varchar(50) NOT NULL,
`nationalite` int(11) NOT NULL,
`pwd` text NOT NULL,
PRIMARY KEY (`id`),
KEY `idDroit` (`idDroit`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Structure de la table `produit`
--
CREATE TABLE IF NOT EXISTS `produit` (
`id` int(11) NOT NULL,
`nom_Produit` varchar(100) NOT NULL,
`prix` float NOT NULL,
`description` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `refugie`
--
CREATE TABLE IF NOT EXISTS `refugie` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(50) NOT NULL,
`prenom` varchar(50) NOT NULL,
`date_nais` date NOT NULL,
`grpe_sanguin` varchar(10) NOT NULL,
`photo` varchar(150) NOT NULL,
`nationalite` varchar(20) NOT NULL,
`retrouve` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Structure de la table `sejour`
--
CREATE TABLE IF NOT EXISTS `sejour` (
`idRefugie` int(11) NOT NULL,
`idCamps` int(11) NOT NULL,
`date_arr` datetime NOT NULL,
`date_dep` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `stock`
--
CREATE TABLE IF NOT EXISTS `stock` (
`idProduit` int(11) NOT NULL,
`idInfrastructure` int(11) NOT NULL,
`quantite` int(11) NOT NULL,
KEY `idInfrastructure` (`idInfrastructure`),
KEY `idProduit` (`idProduit`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `travaille`
--
CREATE TABLE IF NOT EXISTS `travaille` (
`idPersonnel` int(11) NOT NULL,
`idInfrastructure` int(11) NOT NULL,
`date_arr` datetime NOT NULL,
`date_dep` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Contraintes pour les tables exportées
--
--
-- Contraintes pour la table `commande_produit`
--
ALTER TABLE `commande_produit`
ADD CONSTRAINT `commande_produit_ibfk_2` FOREIGN KEY (`idProduit`) REFERENCES `produit` (`id`),
ADD CONSTRAINT `commande_produit_ibfk_1` FOREIGN KEY (`idCommande`) REFERENCES `commande` (`id`);
--
-- Contraintes pour la table `stock`
--
ALTER TABLE `stock`
ADD CONSTRAINT `stock_ibfk_1` FOREIGN KEY (`idProduit`) REFERENCES `produit` (`id`);
--
-- Contraintes pour la table `personnel`
--
ALTER TABLE `personnel`
ADD CONSTRAINT `personnel_ibfk_1` FOREIGN KEY (`idDroit`) REFERENCES `droit` (`id`);
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
thanks for your help.
thomas

MySql and creating Foreign keys

-- MySQL Script generated by MySQL Workbench
-- Sun Apr 2 17:50:54 2017
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema SintalDB
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema SintalDB
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `SintalDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_slovenian_ci ;
USE `SintalDB` ;
-- -----------------------------------------------------
-- Table `SintalDB`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`User` (
`PK_User` INT NOT NULL,
`Name` VARCHAR(15) NOT NULL,
`Surname` VARCHAR(25) NOT NULL,
`Phone_Nr` VARCHAR(13) NOT NULL,
`Email` VARCHAR(90) NOT NULL,
`Car_Reg_Nr` VARCHAR(10) NULL,
`Username` VARCHAR(20) NOT NULL,
`Password` VARCHAR(20) NOT NULL,
`Servis_Input_Rights` TINYINT(1) NOT NULL,
`Servis_Output_Rights` TINYINT(1) NOT NULL,
`Servis_Delete_Rights` TINYINT(1) NOT NULL,
`Admin_Rights` TINYINT(1) NOT NULL,
PRIMARY KEY (`PK_User`))
ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`City`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`City` (
`PK_City` INT NOT NULL,
`Name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`PK_City`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`Company`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Company` (
`PK_Company` INT NOT NULL,
`Name` VARCHAR(25) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
`Address_Nr` VARCHAR(4) NOT NULL,
`FK_City` INT NOT NULL,
PRIMARY KEY (`PK_Company`),
INDEX `FK_City_Company_fkn_idx` (`FK_City` ASC),
CONSTRAINT `FK_City_Company_fkn`
FOREIGN KEY (`FK_City`)
REFERENCES `SintalDB`.`City` (`PK_City`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`Facility`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Facility` (
`PK_Facility_AD` VARCHAR(6) NOT NULL,
`Name` VARCHAR(60) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
`Address_Nr` VARCHAR(4) NOT NULL,
`FK_City` INT NOT NULL,
`FK_Company_Affiliation` INT NOT NULL,
PRIMARY KEY (`PK_Facility_AD`),
INDEX `FK_City_Facility_fkn_idx` (`FK_City` ASC),
INDEX `FK_Company_Affiliation_Facility_fkn_idx` (`FK_Company_Affiliation` ASC),
CONSTRAINT `FK_City_Facility_fkn`
FOREIGN KEY (`FK_City`)
REFERENCES `SintalDB`.`City` (`PK_City`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_Company_Affiliation_Facility_fkn`
FOREIGN KEY (`FK_Company_Affiliation`)
REFERENCES `SintalDB`.`Company` (`PK_Company`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`Servis`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Servis` (
`PK_Servis_Nr` VARCHAR(11) NOT NULL,
`FK_Facility_AD` VARCHAR(6) NOT NULL,
`Description` VARCHAR(255) NOT NULL,
`Date_Recived` DATETIME NOT NULL,
`FK_User_Commited` INT NULL,
`Date_Commited` DATETIME NULL,
`FK_User_Done` INT NULL,
`Date_Done` DATETIME NULL,
PRIMARY KEY (`PK_Servis_Nr`),
INDEX `FK_User_Servis_fkn_idx` (`FK_User_Commited` ASC, `FK_User_Done` ASC),
INDEX `FK_Facility_Servis_fkn_idx` (`FK_Facility_AD` ASC),
CONSTRAINT `FK_User_Servis_fkn`
FOREIGN KEY (`FK_User_Commited` , `FK_User_Done`)
REFERENCES `SintalDB`.`User` (`PK_User` , `PK_User`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_Facility_Servis_fkn`
FOREIGN KEY (`FK_Facility_AD`)
REFERENCES `SintalDB`.`Facility` (`PK_Facility_AD`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
-- -----------------------------------------------------
-- Table `SintalDB`.`ServisNote`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`ServisNote` (
`PK_ServisNote` INT NOT NULL,
`Note` VARCHAR(255) NOT NULL,
`FK_User_Note` INT NOT NULL,
`Date_Created` DATETIME NOT NULL,
`FK_Servis_Nr` VARCHAR(11) NOT NULL,
PRIMARY KEY (`PK_ServisNote`),
INDEX `FK_Servis_ServisNote_fkn_idx` (`FK_Servis_Nr` ASC),
INDEX `FK_User_ServisNote_fkn_idx` (`FK_User_Note` ASC),
CONSTRAINT `FK_Servis_ServisNote_fkn`
FOREIGN KEY (`FK_Servis_Nr`)
REFERENCES `SintalDB`.`Servis` (`PK_Servis_Nr`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_User_ServisNote_fkn`
FOREIGN KEY (`FK_User_Note`)
REFERENCES `SintalDB`.`User` (`PK_User`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
this is my sql file and I get error after i run sql file
Error Code: 1215. Cannot add foreign key constraint
17:52:08 CREATE TABLE IF NOT EXISTS SintalDB.Servis (
PK_Servis_Nr VARCHAR(11) NOT NULL, FK_Facility_AD VARCHAR(6) NOT
NULL, Description VARCHAR(255) NOT NULL, Date_Recived DATETIME
NOT NULL, FK_User_Commited INT NULL, Date_Commited DATETIME
NULL, FK_User_Done INT NULL, Date_Done DATETIME NULL,
PRIMARY KEY (PK_Servis_Nr), INDEX FK_User_Servis_fkn_idx
(FK_User_Commited ASC, FK_User_Done ASC), INDEX
FK_Facility_Servis_fkn_idx (FK_Facility_AD ASC), CONSTRAINT
FK_User_Servis_fkn FOREIGN KEY (FK_User_Commited ,
FK_User_Done) REFERENCES SintalDB.User (PK_User ,
PK_User) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT FK_Facility_Servis_fkn FOREIGN KEY (FK_Facility_AD)
REFERENCES SintalDB.Facility (PK_Facility_AD) ON DELETE NO
ACTION ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET
= utf8 COLLATE = utf8_slovenian_ci Error Code: 1215. Cannot add foreign key constraint 0.063 sec
Check this: Adding foreign key on multiple columns
I think you must add two foreign key for made reference to the same column.
You can replace your code for this and test.
-- -----------------------------------------------------
-- Table `SintalDB`.`Servis`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Servis` (
`PK_Servis_Nr` VARCHAR(11) NOT NULL,
`FK_Facility_AD` VARCHAR(6) NOT NULL,
`Description` VARCHAR(255) NOT NULL,
`Date_Recived` DATETIME NOT NULL,
`FK_User_Commited` INT NULL,
`Date_Commited` DATETIME NULL,
`FK_User_Done` INT NULL,
`Date_Done` DATETIME NULL,
PRIMARY KEY (`PK_Servis_Nr`),
INDEX `FK_User_Servis_fkn_idx` (`FK_User_Commited` ASC, `FK_User_Done` ASC),
INDEX `FK_Facility_Servis_fkn_idx` (`FK_Facility_AD` ASC),
CONSTRAINT `FK_User_Servis_fkn`
FOREIGN KEY (`FK_User_Commited`)
REFERENCES `SintalDB`.`User` (`PK_User` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_User_Servis_2_fkn`
FOREIGN KEY (`FK_User_Done`)
REFERENCES `SintalDB`.`User` (`PK_User`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_Facility_Servis_fkn`
FOREIGN KEY (`FK_Facility_AD`)
REFERENCES `SintalDB`.`Facility` (`PK_Facility_AD`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;

Getting a Cannot add Foreign Key Constraint MySQL

I am having some trouble with phpMyAdmin and MySQL. All of the tables load just fine except for the order table. No matter if I do it all at once, or one table at a time, I get a #1215 - Cannot add foreign key constraint.
This happens for the Orders table only and the Customer_Number attribute. What in the world am I missing here. Thanks in advance.
-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Apr 19, 2015 at 01:22 AM
-- Server version: 5.6.21
-- PHP Version: 5.6.3
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
SET foreign_key_checks=0;
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
-- Database: `popcorn`
-- --------------------------------------------------------
-- Table structure for table `customer`
CREATE TABLE IF NOT EXISTS `customer` (
`Scout_Number` int(10) NOT NULL,
`Customer_Number` int(10) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`House_Number` int(7) NOT NULL,
`Street` varchar(15) NOT NULL,
`City` varchar(15) NOT NULL,
`State` char(2) NOT NULL,
`Zip` int(5) NOT NULL,
`Phone` int(10) NOT NULL,
`Email` varchar(30) NOT NULL,
PRIMARY KEY (Scout_Number, Customer_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `den`
CREATE TABLE IF NOT EXISTS `den` (
`Den_Number` int(3) NOT NULL,
`User_Name` varchar(8) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`Phone` int(10) NOT NULL,
`Email` varchar(30) NOT NULL,
`Den_City` varchar(15) NOT NULL,
`Sales_Goal` int(10) NOT NULL,
`Den_Sales_Total` decimal(10,2) NOT NULL,
`Den_State` char(2) NOT NULL,
PRIMARY KEY (Den_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table `den`
-- Table structure for table `order`
CREATE TABLE IF NOT EXISTS `order` (
`Order_Number` int(10) NOT NULL,
`Customer_Number` int(10) NOT NULL,
`Donation` decimal(5,2) NOT NULL,
`Order_Total` decimal(5,2) NOT NULL,
`Payment_Status` char(1) NOT NULL,
`Payment_Type` varchar(10) NOT NULL,
`Date` date NOT NULL,
`Delivery_Status` char(1) NOT NULL,
PRIMARY KEY (Order_Number),
FOREIGN KEY (Customer_Number) REFERENCES customer(Customer_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `order_product`
CREATE TABLE IF NOT EXISTS `order_product` (
`Order_Number` int(10) NOT NULL,
`Product_Number` int(10) NOT NULL,
PRIMARY KEY (Order_Number, Product_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `product`
CREATE TABLE IF NOT EXISTS `product` (
`Product_Number` int(10) NOT NULL,
`Product_Name` varchar(15) NOT NULL,
`Description` text NOT NULL,
`Image` blob NOT NULL,
`Price` decimal(5,2) NOT NULL,
PRIMARY KEY (Product_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `scout`
CREATE TABLE IF NOT EXISTS `scout` (
`Scout_Number` int(10) NOT NULL,
`User_Name` char(8) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`Sales_Goal` decimal(10,2) NOT NULL,
`Prize_Progress` int(10) NOT NULL,
`Den_Number` int(3) NOT NULL,
PRIMARY KEY (Scout_Number),
FOREIGN KEY (Den_Number) REFERENCES den(Den_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Columns referenced in a foreign key have to be indexed. You don't have an index on Customer_Number in the customer table. Either change the order of the columns in the composite primary key to(Customer_Number, Scout_Number)or add an additional key just on theCustomer_Number` column.
Note, however, that having a foreign key pointing to a non-unique column is a MySQL extension to SQL, and likely to be a bad idea. See Can a foreign key reference a non-unique index?. I wonder why the primary key of the customer table isn't just Customer_Number.

Error 1005: Can't create table can't find where the issue is

I have created a EER model using MySQL Workbench (latest release). Apparently all is fine but when I try to do a FORWARD ENGINEER I get the common Error 1005: Can't create table. As suggested in this post I check every step on the first answer but without success, can any give some extra eyes to my query and try to find where my error is? I can't see it.
This is the error generated by the tool:
Executing SQL script in server
ERROR: Error 1005: Can't create table
'portal_de_logistica.order_has_comment' (errno: 150)
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`order_has_comment` (
`user_id` INT(11) NOT NULL,
`no_order` VARCHAR(15) NOT NULL,
`comment` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`, `no_order`),
CONSTRAINT `fk_fos_user_rnd`
FOREIGN KEY (`user_id`)
REFERENCES `portal_de_logistica`.`fos_user_user` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `fk_tb_order_rnd`
FOREIGN KEY (`no_order`)
REFERENCES `portal_de_logistica`.`tb_order` (`no_order`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci
And this is the complete SQL sentences:
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
DROP SCHEMA IF EXISTS `portal_de_logistica` ;
CREATE SCHEMA IF NOT EXISTS `portal_de_logistica` DEFAULT CHARACTER SET latin1 ;
USE `portal_de_logistica` ;
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`company`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`company` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`company` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) NOT NULL,
`register_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`country` CHAR(3) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`contact`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`contact` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`contact` (
`id` INT NOT NULL AUTO_INCREMENT,
`company_id` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(90) NOT NULL,
`c_position` VARCHAR(150) NULL,
`email` VARCHAR(150) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_contact_company1`
FOREIGN KEY (`company_id`)
REFERENCES `portal_de_logistica`.`company` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
CREATE INDEX `fk_contact_company1_idx` ON `portal_de_logistica`.`contact` (`company_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`tb_order`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`tb_order` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`tb_order` (
`no_order` VARCHAR(15) NOT NULL,
`company_id` INT NOT NULL,
`business_case` VARCHAR(15) NULL,
`charge_status` ENUM('0','1','2','3','4','5','6','7','8') NOT NULL COMMENT '0: Ninguno\n1: Proceso de Fabricacion\n2: Pickup en destino\n3: A la espera de recojo por cliente\n4: Carga en transito\n5: Carga arribada\n6: En proceso de aduana\n7: Entregado a cliente\n8: En bodega',
`eta` DATE NULL,
`etd` DATE NULL,
`transport_media` ENUM('0','1','2') NOT NULL COMMENT '0: EXW\n1: Maritimo' /* comment truncated */ /*2: Aereo*/,
`incoterm` ENUM('0','1','2','3','4') NULL COMMENT '0: Ninguno\n1: EWX\n2: FOB\n3: CIF' /* comment truncated */ /*4: DDP*/,
PRIMARY KEY (`no_order`),
CONSTRAINT `fk_order_company1`
FOREIGN KEY (`company_id`)
REFERENCES `portal_de_logistica`.`company` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB;
CREATE UNIQUE INDEX `no_order_UNIQUE` ON `portal_de_logistica`.`tb_order` (`no_order` ASC);
CREATE INDEX `fk_order_company1_idx` ON `portal_de_logistica`.`tb_order` (`company_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`fos_user_group`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`fos_user_group` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`fos_user_group` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`roles` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL COMMENT '(DC2Type:array)',
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE UNIQUE INDEX `UNIQ_583D1F3E5E237E06` ON `portal_de_logistica`.`fos_user_group` (`name` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`fos_user_user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`fos_user_user` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`fos_user_user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`username_canonical` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`email` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`email_canonical` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`enabled` TINYINT(1) NOT NULL,
`salt` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`password` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`last_login` DATETIME NULL DEFAULT NULL,
`locked` TINYINT(1) NOT NULL,
`expired` TINYINT(1) NOT NULL,
`expires_at` DATETIME NULL DEFAULT NULL,
`confirmation_token` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`password_requested_at` DATETIME NULL DEFAULT NULL,
`roles` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL COMMENT '(DC2Type:array)',
`credentials_expired` TINYINT(1) NOT NULL,
`credentials_expire_at` DATETIME NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
`date_of_birth` DATETIME NULL DEFAULT NULL,
`firstname` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`lastname` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`website` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`biography` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`gender` VARCHAR(1) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`locale` VARCHAR(8) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`timezone` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`phone` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`facebook_uid` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`facebook_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`facebook_data` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL COMMENT '(DC2Type:json)',
`twitter_uid` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`twitter_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`twitter_data` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL COMMENT '(DC2Type:json)',
`gplus_uid` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`gplus_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`gplus_data` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL COMMENT '(DC2Type:json)',
`token` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`two_step_code` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE UNIQUE INDEX `UNIQ_C560D76192FC23A8` ON `portal_de_logistica`.`fos_user_user` (`username_canonical` ASC);
CREATE UNIQUE INDEX `UNIQ_C560D761A0D96FBF` ON `portal_de_logistica`.`fos_user_user` (`email_canonical` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`fos_user_user_group`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`fos_user_user_group` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`fos_user_user_group` (
`user_id` INT(11) NOT NULL,
`group_id` INT(11) NOT NULL,
PRIMARY KEY (`user_id`, `group_id`),
CONSTRAINT `FK_B3C77447FE54D947`
FOREIGN KEY (`group_id`)
REFERENCES `portal_de_logistica`.`fos_user_group` (`id`)
ON DELETE CASCADE,
CONSTRAINT `FK_B3C77447A76ED395`
FOREIGN KEY (`user_id`)
REFERENCES `portal_de_logistica`.`fos_user_user` (`id`)
ON DELETE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE INDEX `IDX_B3C77447A76ED395` ON `portal_de_logistica`.`fos_user_user_group` (`user_id` ASC);
CREATE INDEX `IDX_B3C77447FE54D947` ON `portal_de_logistica`.`fos_user_user_group` (`group_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`media__gallery`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`media__gallery` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`media__gallery` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`context` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`default_format` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`enabled` TINYINT(1) NOT NULL,
`updated_at` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`media__media`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`media__media` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`media__media` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`description` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`enabled` TINYINT(1) NOT NULL,
`provider_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`provider_status` INT(11) NOT NULL,
`provider_reference` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`provider_metadata` LONGTEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL COMMENT '(DC2Type:json)',
`width` INT(11) NULL DEFAULT NULL,
`height` INT(11) NULL DEFAULT NULL,
`length` DECIMAL(10,0) NULL DEFAULT NULL,
`content_type` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`content_size` INT(11) NULL DEFAULT NULL,
`copyright` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`author_name` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`context` VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NULL DEFAULT NULL,
`cdn_is_flushable` TINYINT(1) NULL DEFAULT NULL,
`cdn_flush_at` DATETIME NULL DEFAULT NULL,
`cdn_status` INT(11) NULL DEFAULT NULL,
`updated_at` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`media__gallery_media`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`media__gallery_media` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`media__gallery_media` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`gallery_id` INT(11) NULL DEFAULT NULL,
`media_id` INT(11) NULL DEFAULT NULL,
`position` INT(11) NOT NULL,
`enabled` TINYINT(1) NOT NULL,
`updated_at` DATETIME NOT NULL,
`created_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_80D4C541EA9FDD75`
FOREIGN KEY (`media_id`)
REFERENCES `portal_de_logistica`.`media__media` (`id`),
CONSTRAINT `FK_80D4C5414E7AF8F`
FOREIGN KEY (`gallery_id`)
REFERENCES `portal_de_logistica`.`media__gallery` (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE INDEX `IDX_80D4C5414E7AF8F` ON `portal_de_logistica`.`media__gallery_media` (`gallery_id` ASC);
CREATE INDEX `IDX_80D4C541EA9FDD75` ON `portal_de_logistica`.`media__gallery_media` (`media_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`fos_user_user_has_company`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`fos_user_user_has_company` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`fos_user_user_has_company` (
`fos_user_user_id` INT(11) NOT NULL,
`company_id` INT NOT NULL,
PRIMARY KEY (`fos_user_user_id`, `company_id`),
CONSTRAINT `fk_fos_user_user_has_company_fos_user_user1`
FOREIGN KEY (`fos_user_user_id`)
REFERENCES `portal_de_logistica`.`fos_user_user` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `fk_fos_user_user_has_company_company1`
FOREIGN KEY (`company_id`)
REFERENCES `portal_de_logistica`.`company` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE INDEX `fk_fos_user_user_has_company_company1_idx` ON `portal_de_logistica`.`fos_user_user_has_company` (`company_id` ASC);
CREATE INDEX `fk_fos_user_user_has_company_fos_user_user1_idx` ON `portal_de_logistica`.`fos_user_user_has_company` (`fos_user_user_id` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`order_has_media__media`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`order_has_media__media` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`order_has_media__media` (
`order_no_order` VARCHAR(15) NOT NULL,
`media__media_id` INT(11) NOT NULL,
PRIMARY KEY (`order_no_order`, `media__media_id`),
CONSTRAINT `fk_order_has_media__media_order1`
FOREIGN KEY (`order_no_order`)
REFERENCES `portal_de_logistica`.`tb_order` (`no_order`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_order_has_media__media_media__media1`
FOREIGN KEY (`media__media_id`)
REFERENCES `portal_de_logistica`.`media__media` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_order_has_media__media_media__media1_idx` ON `portal_de_logistica`.`order_has_media__media` (`media__media_id` ASC);
CREATE INDEX `fk_order_has_media__media_order1_idx` ON `portal_de_logistica`.`order_has_media__media` (`order_no_order` ASC);
-- -----------------------------------------------------
-- Table `portal_de_logistica`.`order_has_comment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `portal_de_logistica`.`order_has_comment` ;
CREATE TABLE IF NOT EXISTS `portal_de_logistica`.`order_has_comment` (
`user_id` INT(11) NOT NULL,
`no_order` VARCHAR(15) NOT NULL,
`comment` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`, `no_order`),
CONSTRAINT `fk_fos_user_rnd`
FOREIGN KEY (`user_id`)
REFERENCES `portal_de_logistica`.`fos_user_user` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `fk_tb_order_rnd`
FOREIGN KEY (`no_order`)
REFERENCES `portal_de_logistica`.`tb_order` (`no_order`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
CREATE INDEX `fk_fos_user_user_has_tb_order_tb_order1_idx` ON `portal_de_logistica`.`order_has_comment` (`no_order` ASC);
CREATE INDEX `fk_fos_user_user_has_tb_order_fos_user_user1_idx` ON `portal_de_logistica`.`order_has_comment` (`user_id` ASC);
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
The issue is that tb_order doesn't specify a charset or collation like the other tables so will use the database default.
If the default is different from utf8 and utf8_unicode_ci then the foreign key on a text field wont work.