I have a problem with my Database (using MariaDB) to insert a new row
I Having the following errors
Cannot add or update a child row: a foreign key constraint fails
(DbName_boutique.Commande, CONSTRAINT bdc_managed_by___fk FOREIGN
KEY (bdc_managed_by_user_id) REFERENCES utilisateur
(utilisateur_id))INSERT INTO Commande (lien_devis, lien_bdc,
end_devis, bdc_status, devis_created_by_user_id,
bdc_managed_by_user_id, entreprise_id)
I have this code on my controller :
$unknow = NULL;
$end_date = date('d/m/Y', strtotime("+1 month"));
$user = checkConnected($bdd);
Commande::create($bdd, $new_full_modele_devis_path, $new_full_modele_devis_path, $end_date, 0, $user->getId(), $user->getId(), $entreprise->getId());
The $unknow are values I used later in an another Controller.
My Table "Commande" is built like following :
create table dBName.Commande
(
commande_id int not null
primary key,
lien_devis varchar(255) null,
lien_bdc varchar(255) null,
end_devis datetime null,
bdc_status int null,
devis_created_by_user_id mediumint unsigned null,
bdc_managed_by_user_id mediumint unsigned null,
entreprise_id smallint(5) unsigned null,
constraint bdc_managed_by___fk
foreign key (bdc_managed_by_user_id) references dBName.utilisateur (utilisateur_id),
constraint devis_created_by___fk
foreign key (devis_created_by_user_id) references dBName.utilisateur (utilisateur_id),
constraint entreprise___fk
foreign key (entreprise_id) references dBName.entreprise (entreprise_id)
);
And I have later the user table :
create table dBName.utilisateur
(
utilisateur_id mediumint unsigned auto_increment
primary key,
utilisateur_password varchar(255) null,
utilisateur_nom varchar(50) null,
utilisateur_prenom varchar(50) null,
utilisateur_email varchar(255) null,
utilisateur_telephone varchar(10) null,
utilisateur_fax varchar(25) null,
is_active tinyint(1) null
)
charset = utf8mb4;
And entreprise :
create table dBName.entreprise
(
entreprise_id smallint(5) unsigned auto_increment
primary key,
entreprise_nom varchar(100) null,
entreprise_siret char(14) null,
entreprise_telephone char(10) null,
entreprise_salesforce_number varchar(100) null,
entreprise_compte_client varchar(100) null,
entreprise_raison_sociale varchar(100) null,
entreprise_APE varchar(25) null,
entreprise_image_link varchar(255) null,
adresse_id mediumint unsigned null,
constraint FK_entreprise_adresse_id
foreign key (adresse_id) references dBName.adresse (adresse_id)
)
charset = utf8mb4;
I don't understand Why i'm Habing the error... And bit of help could help me a lot.. Thank you.
In Commande table, the column bdc_managed_by_user_id is mapped to utilisateur_id column of utilisateur table via foreign key reference.
This means, whenever you insert a row in Commande table, corresponding bdc_managed_by_user_id has to be one of the utilisateur_id present in utilisateur table. If not, you will get the above error.
This is called Foreign Key Constraint and you can have a look at this example describing how it works.
Related
This is my t_complaint
CREATE TABLE `t_complaint` (
`idcomplaint` int(11) NOT NULL,
`tglterima` date DEFAULT NULL,
`dept` varchar(5) DEFAULT NULL,
`pengirim` varchar(255) DEFAULT NULL,
`kontak` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`telp` varchar(255) DEFAULT NULL,
`jenis` varchar(45) DEFAULT NULL,
`uraian` text,
`uniqueid` varchar(9) DEFAULT NULL,
`responder` varchar(245) DEFAULT NULL,
`tgljawab` date DEFAULT NULL,
`jawaban` text,
`status` varchar(45) DEFAULT NULL,
`tglclose` date DEFAULT NULL,
`createddate` datetime DEFAULT NULL,
`createdby` varchar(45) DEFAULT NULL,
`modifieddate` datetime DEFAULT NULL,
`modifiedby` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and this is t_complaint_detail:
CREATE TABLE `t_complaint_detail` (
`no` int(11) NOT NULL,
`uniqueid` varchar(9) DEFAULT NULL,
`uploader` varchar(100) DEFAULT NULL,
`st_uploader` int(11) DEFAULT NULL,
`file_upload` text,
`original_name` text,
`status` int(11) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
t_complaint.idcomplaint is a primary key with Auto Increment attribute
t_complaint_detail.no is a primary key with Auto Increment attribute
I'd like to connected these table via uniqueid
I've tried ALTER TABLE t_complaint_detail ADD CONSTRAINT fk_unique FOREIGN KEY ('uniqueid') REFERENCES t_complaint('uniqueid')
The query above gives error. The error is #1005 - Can't create tablebsm.#sql-890_730(errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
bsm in the error is my database (my database is bsm)
Foreign key should must be a primary key in parent table. The question is, why you are adding a redundant 'uniqueId' in both the table? You can simply define a foreign key constraint like this :
t_complaint_detail ADD CONSTRAINT fk_unique FOREIGN KEY ('idcomplaint') REFERENCES t_complaint('idcomplaint').
It should work.
Error came like this
ERROR 1215 (HY000): Cannot add foreign key constraint
Because you need to create primary key in table.you can add primary key
like this
alter table t_complaint_detail modify uniqueid varchar(20) not null;
and
alter table t_complaint_detail modify uniqueid varchar(20) not null;
after it will be works
ALTER TABLE t_complaint_detail ADD CONSTRAINT fk_unique FOREIGN KEY (uniqueid) REFERENCES t_complaint(uniqueid);
please read here for more reference that will be help
It's working fine for me :
Try this :
ALTER TABLE t_complaint ADD CONSTRAINT t_complaint_detail
FOREIGN KEY ( uniqueid ) REFERENCES t_complaint( uniqueid )
Reference here :
https://www.w3schools.com/sql/sql_foreignkey.asp
I am having a problem trying to load my database into MySQL as I am having a few errors with my foreign keys. Things I have tried to do to fix this issue is:
- Putting foreign keys after the primary keys
Eg:
CREATE TABLE IF NOT EXISTS Passenger (
.......
PRIMARY KEY(tNum),
FOREIGN KEY (fNum) REFERENCES Flights(fNum),
FOREIGN KEY (fDate) REFERENCES Flights(fDate),
FOREIGN KEY (sCity) REFERENCES Flights(sCity),
FOREIGN KEY (eCity) REFERENCES Flights(eCity)
);
- Bunching the foreign keys in the format of
Eg:
ALTER TABLE Passenger ADD FOREIGN KEY (fNum, fDate, sCity, eCity) REFERENCES Flights(fNum, fDate, sCity, eCity);
The error I get is:
1005 - Can't create table 'airline.#sql-1d7_7c' (errno: 150)
My full code is:
DROP DATABASE IF EXISTS airline;
CREATE DATABASE IF NOT EXISTS airline;
USE airline;
CREATE TABLE IF NOT EXISTS Flights (
fNum char(6) not null,
pID char(4) not null,
fDate DATE not null,
eDate DATE not null,
sTime char(4) not null,
lTime char(4) not null,
sOStart char(4) null,
sOEnd char(4) null,
sCity varchar(30) not null,
eCity varchar(30) not null,
sOCity varchar(30),
sNum char(5) not null,
PRIMARY KEY (fNum)
);
CREATE TABLE IF NOT EXISTS Passenger (
tNum char(4) not null,
dPurch DATE not null,
pMethod varchar(30) not null,
fNum char(6) not null,
fDate DATE not null,
sCity varchar(30) not null,
eCity varchar(30) not null,
tType varchar(30) not null,
Price decimal(4,2) not null,
iType varchar(30) not null,
idNum char(8) not null,
fName varchar(30) not null,
lName varchar(30) not null,
Sex char(1) not null,
pAddress varchar(30) not null,
pPhone char(8) not null,
pEmail varchar(30) not null,
PRIMARY KEY(tNum)
);
CREATE TABLE IF NOT EXISTS Planes (
pID char(4) not null,
pType char(3) not null,
pDesc varchar(30) not null,
pRange char(4) not null,
Capacity char(3) not null,
mDate DATE not null,
pDate DATE not null,
sDate DATE not null,
PRIMARY KEY (pID)
);
CREATE TABLE IF NOT EXISTS Staff (
sNum char(5) not null,
sName varchar(30) not null,
sDOB DATE not null,
sAddress varchar(30) not null,
pCompany varchar(30) ,
pStart DATE ,
pEnd DATE ,
jID char(1) not null,
PRIMARY KEY (sNum)
);
CREATE TABLE IF NOT EXISTS Emergency (
eID char(5) not null,
sNum char(5) not null,
eName varchar(30) not null,
eAddress varchar(30) not null,
ePhone char(8) not null,
eEmail varchar(30) not null,
eRelationship varchar(30) not null,
PRIMARY KEY(eID)
);
CREATE TABLE IF NOT EXISTS Pilot (
sNum char(5) not null,
pID char(4) not null,
cDate DATE not null,
jID char(1) not null,
PRIMARY KEY(jID)
);
CREATE TABLE IF NOT EXISTS Attendant (
sNum char(5) not null,
tSDate Date not null,
tFDate Date not null,
tDesc Varchar(30) not null,
jID Char(1) not null,
PRIMARY KEY(jID)
);
ALTER TABLE Flights ADD FOREIGN KEY (pID) REFERENCES Planes(pID);
ALTER TABLE Flights ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Passenger ADD FOREIGN KEY (fNum) REFERENCES Flights(fNum);
ALTER TABLE Passenger ADD FOREIGN KEY (fDate) REFERENCES Flights(fDate);
ALTER TABLE Passenger ADD FOREIGN KEY (sCity) REFERENCES Flights(sCity);
ALTER TABLE Passenger ADD FOREIGN KEY (eCity) REFERENCES Flights(eCity);
ALTER TABLE Emergency ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Pilot ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Pilot ADD FOREIGN KEY (pID) REFERENCES Planes(pID);
ALTER TABLE Pilot ADD FOREIGN KEY (jID) REFERENCES Staff(jID);
ALTER TABLE Attendant ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Attendant ADD FOREIGN KEY (jID) REFERENCES Staff(jID);
For me this is the one it fails on first
ALTER TABLE Passenger ADD FOREIGN KEY (fDate) REFERENCES Flights(fDate);
Try adding an index on Flights.fdate first, then doing the foreign key and doing that for additional references until it works. Let me know if that works for you, it did for me.
By the way you might want to reconsider your schema there. Joins and relations should really be done on integers only. That schema will bog down fast as it grows. Also, and maybe this is just personal preference but I prefer first_name to fName. Easier to read for other developers, this isn't 1986, we can have human readbable names now :-)
I try to relate tables mysql but I have this error
Error Code: 1215. Cannot add foreign key constraint
this is the mysql script
CREATE DATABASE prototipo;
USE prototipo;
CREATE TABLE tb_tipo_usuario(
id int not null,
tipo varchar(30) not null,
constraint PK_tb_tipo_usuario_id primary key(id)
);
CREATE TABLE tb_usuarios(
id_usuario int(5) zerofill not null auto_increment,
usuario varchar(30) not null,
clave varchar(30) not null,
nombre varchar(30) not null,
apellido varchar(30) not null,
cedula varchar(30) not null,
cargo varchar(40) not null,
tipo_usuario int not null,
fecha_registro date not null,
constraint PK_tb_usuarios_id_usuario primary key(id_usuario),
constraint fk_tb_usuarios_tipo_usuario foreign key(tipo_usuario) references tb_tipo_usuario(id)
);
CREATE TABLE tb_tipo_pollo(
id_tipo_pollo int(2) zerofill not null auto_increment,
tipo_pollo varchar(30) not null,
constraint pk_tb_tipo_pollo_id_tipo_pollo primary key(id_tipo_pollo)
);
create table tb_parvada(
id_parvada int(5) zerofill not null,
cantidad int not null,
constraint pk_tb_parvada_id_parvada primary key(id_parvada)
);
create table tb_entrada_parvada(
id_entrada_parvada int(5) zerofill not null auto_increment,
tipo_pollo int not null,
cantidad int not null,
fecha_entrada date not null,
fecha_registro TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
comentario varchar(500) null,
id_usuario int not null,
constraint pk_tb_entrada_parvada_id_entrada_pollo primary key(id_entrada_parvada),
constraint fk_tb_entrada_parvada_tipo_pollo foreign key(tipo_pollo) references tb_tipo_pollo(id_tipo_pollo),
constraint fk_tb_entrada_parvada_id_entrada_parvada foreign key(id_entrada_parvada) references tb_parvada(id_parvada),
constraint fk_tb_entrada_parvada_id_usuario foreign key(id_usuario) references tb_usuarios(id_usuario)
);
create table tb_muerte_pollo(
id_muerte int(5) zerofill not null auto_increment,
id_parvada int not null,
cantidad int not null,
fecha date,
fecha_registro TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
motivo varchar(300),
id_usuario int not null,
constraint pk_tb_muerte_pollo primary key(id_muerte),
constraint fk_tb_muerte_pollo foreign key(id_parvada) references tb_parvada(id_parvada),
constraint fk_tb_muerte_pollo_id_usuario foreign key(id_usuario) references tb_usuarios(id_usuario)
);
The error is when I try add the table tb_entrada_parvada or the table tb_muerte_pollo I don't know how repair this error, just work when I delete the foreign key
The DataType and attributes of the column in the reference table should be same as the current table. You are missing ZeroFill for many columns. That why you are getting the error. Kindly Change it.
create table tb_entrada_parvada(
id_entrada_parvada int(5) zerofill not null auto_increment,
tipo_pollo int(2) zerofill not null,
cantidad int not null,
fecha_entrada date not null,
fecha_registro TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
comentario varchar(500) null,
id_usuario int(5) zerofill not null,
constraint pk_tb_entrada_parvada_id_entrada_pollo primary key(id_entrada_parvada),
constraint fk_tb_entrada_parvada_tipo_pollo foreign key(tipo_pollo) references tb_tipo_pollo(id_tipo_pollo),
constraint fk_tb_entrada_parvada_id_entrada_parvada foreign key(id_entrada_parvada) references tb_parvada(id_parvada),
constraint fk_tb_entrada_parvada_id_usuario foreign key(id_usuario) references tb_usuarios(id_usuario)
);
create table tb_muerte_pollo(
id_muerte int(5) zerofill not null auto_increment,
id_parvada int zerofill not null,
cantidad int not null,
fecha date,
fecha_registro TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
motivo varchar(300),
id_usuario int zerofill not null,
constraint pk_tb_muerte_pollo primary key(id_muerte),
constraint fk_tb_muerte_pollo foreign key(id_parvada) references tb_parvada(id_parvada),
constraint fk_tb_muerte_pollo_id_usuario foreign key(id_usuario) references tb_usuarios(id_usuario)
);
DEMO FIDDLE
Integrity constraint violation – yii\db\IntegrityException
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`jts`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`))
The SQL being executed was: INSERT INTO `employee` (`client_code`, `company_name`, `emp_email`, `emp_mobile`, `emp_first_name`, `emp_last_name`) VALUES ('12345678', 'PVPPCOE', 'saurabhkulkarni2010#hotmail.com', '+449029792183', 'Saurabh', 'Kulkarni')
Error Info: Array
(
[0] => 23000
[1] => 1452
[2] => Cannot add or update a child row: a foreign key constraint fails (`jts`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`))
)
I have three tables Client, Employee and create_client, out of which client and employee has two foreign keys.
This problem is showing when I try to insert data from create_client to employee which has exact same field.
What should I do so that I can insert two tables at one i.e create_client and employee
UPDATE-
Table Structure
1)client
CREATE TABLE IF NOT EXISTS `client` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
2)create_client
CREATE TABLE IF NOT EXISTS `create_client` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL,
`emp_email` varchar(45) NOT NULL,
`emp_mobile` varchar(45) NOT NULL,
`emp_first_name` varchar(45) NOT NULL,
`emp_last_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8;
3)employee
CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL,
`emp_email` varchar(45) NOT NULL,
`emp_mobile` varchar(45) NOT NULL,
`emp_first_name` varchar(45) NOT NULL,
`emp_last_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
This is my table structure, first user will create client that means first it will update create_client. Now if user wants he can add many employee under one client code now user will update employee table from Yii2 dynamic form widget.
For using this widget I have created one table call client this will store only client_code and company name remaining data will go in employee table e.g emp_mobile,emp_email, emp_first_name, emp_last_name.
Now this problem is pop up when user first enters data into create_client table.
everything is working between client and employee table user able to enter as many employee as it wants using Yii2 dynamic form Widget but not working for create_client
Apparently you also have a foreign key in the employee table referencing the client_code of the client table, so you can only use client_code values that already exist in the client table.
I don't know what the structure of the create_client table looks like and what its purpose is, but based on your info I assume that you should first insert the data in client, then in employee and finally in create_client, so that every foreign key's value exists in its respective table.
If this is not correct, please post your table structures and the data or queries that could be causing this.
Edit after your comment: I assume that your table structure looks like this:
CREATE TABLE `create_client` (
`client_code` INT UNSIGNED NOT NULL,
`company_name` VARCHAR(255) NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
UNIQUE KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`),
CONSTRAINT `client_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `create_client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`company_name` VARCHAR(255) NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
However, I would suggest to use client as the parent table and move the foreign key constraint to create_client, so that both employee and create_client have a foreign key of client. I would also like to normalise this and get rid of company_name in the child tables:
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `create_client` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
UNIQUE KEY `client_code` (`client_code`),
CONSTRAINT `create_client_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
The next improvement would be to use the employee table for both unique and non-unique client_code inserts. Instead of using 2 identical tables, apart from the unique key, you could do the unique validation in Yii instead of in MySQL. You can specify the table name in the tableName() method of the CreateClient model . This will remain:
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
This should allow you to use the dynamic form, with Client as the main model and Employee as the items model.
On Yii2 using IO Generator (Model) and MySQL I have found the following.
I have seen SET '' instead of SET NULL in update SQL statement(s) on field(s) with foreign key constraints that have caused a "Integrity constraint violation".
My "fix" is to add a default rule on the foreign key field in the model, like so:
public function rules()
{
return [
//
// your other rules
//
[['field_name_with_foreign_key'], 'default', 'value' => null],
];
}
Hope this helps someone.
remove all foreign key checks and simplify database.
the problem lies in your foreign key structure.
learn carefully about primary keys and foreign keys.
make new database again without foreign keys then check.
I'm working a project with Yiiframwork and I have this table in my data base project
CREATE TABLE IF NOT EXISTS `tbl_annonce` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`idEntreprise` tinyint(3) unsigned NOT NULL
COMMENT 'CONSTRAINT FOREIGN KEY (idEntreprise) REFERENCES tbl_entreprise(id)',
`titre` varchar(100) NOT NULL,
`detailleDiscription` varchar(5000) NOT NULL,
`categorie` varchar(100) DEFAULT NULL,
`typePoste` varchar(100) NOT NULL,
`salaireMin` varchar(80) NOT NULL,
`salaireMax` varchar(80) NOT NULL,
`niveauEtude` varchar(80) NOT NULL,
`niveauExperience` varchar(80) NOT NULL,
`langue` varchar(50) DEFAULT NULL,
`poste` varchar(50) NOT NULL,
`pays` varchar(50) NOT NULL,
`ville` varchar(50) NOT NULL,
`adresse` varchar(80) NOT NULL,
`datePublication` timestamp NOT NULL
DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`etat` varchar(50) NOT NULL,
`photo` varchar(255) NULL,
`nometr` text NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_idEntrepriseAnn`
FOREIGN KEY (idEntreprise)
REFERENCES tbl_entreprise(id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
I only use 'comment' because I found it in a tutorial for a yii framework project.
I have got the attribute 'idEntreprise' as FK in my DB but when I entred other 'idEntreprise' that does note exist in my table entreprise it was not problem, instead it must be a problem.
So I added 'identreprise' as Fk and that have this problem now.
Hope you understand my problem :/
can any one helep me plz !!
To define a foreign key on a column, it must fulfil some conditions.
Child and parent column must have same column definition by type,
signed.
Parent column must have an index defined on it.
Make sure that
The column tbl_entreprise.id is indexed.
The column definition idEntreprise tinyint(3) unsigned in tbl_annonce matches
with that of tbl_entreprise.id
Refer to: MySQL: Foregin Key Constaints