Mysql 字段';外键';不';没有默认值
我使用nodeJS和MySQL开发了我的后端,我有三个表,如下所示: fourniseurs:Mysql 字段';外键';不';没有默认值,mysql,node.js,express,foreign-keys,create-table,Mysql,Node.js,Express,Foreign Keys,Create Table,我使用nodeJS和MySQL开发了我的后端,我有三个表,如下所示: fourniseurs: CREATE TABLE fournisseurs ( Codef bigint(21) NOT NULL , Noment varchar(20) COLLATE utf8_unicode_ci NOT NULL, Prenomf varchar(20) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (Codef, Prenomf) ) ENG
CREATE TABLE fournisseurs (
Codef bigint(21) NOT NULL ,
Noment varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Prenomf varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Codef, Prenomf)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE categorie (
Idcat bigint(21) NOT NULL AUTO_INCREMENT,
Nomcat varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Idcat, Nomcat)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE produits (
Codep bigint(21) NOT NULL AUTO_INCREMENT,
Reference bigint(21) NOT NULL,
Nomp varchar(20) COLLATE utf8_unicode_ci NOT NULL ,
Codef bigint(21) NOT NULL ,
Prenomf varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Idcat bigint(21) NOT NULL ,
Nomcat varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Description varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Codep ),
FOREIGN KEY (Codef, Prenomf) REFERENCES fournisseurs (Codef, Prenomf)
ON DELETE CASCADE
ON UPDATE CASCADE ,
FOREIGN KEY (Idcat, Nomcat) REFERENCES categorie (Idcat, Nomcat)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10;
分类:
CREATE TABLE fournisseurs (
Codef bigint(21) NOT NULL ,
Noment varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Prenomf varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Codef, Prenomf)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE categorie (
Idcat bigint(21) NOT NULL AUTO_INCREMENT,
Nomcat varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Idcat, Nomcat)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE produits (
Codep bigint(21) NOT NULL AUTO_INCREMENT,
Reference bigint(21) NOT NULL,
Nomp varchar(20) COLLATE utf8_unicode_ci NOT NULL ,
Codef bigint(21) NOT NULL ,
Prenomf varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Idcat bigint(21) NOT NULL ,
Nomcat varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Description varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Codep ),
FOREIGN KEY (Codef, Prenomf) REFERENCES fournisseurs (Codef, Prenomf)
ON DELETE CASCADE
ON UPDATE CASCADE ,
FOREIGN KEY (Idcat, Nomcat) REFERENCES categorie (Idcat, Nomcat)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10;
产品:
CREATE TABLE fournisseurs (
Codef bigint(21) NOT NULL ,
Noment varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Prenomf varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Codef, Prenomf)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE categorie (
Idcat bigint(21) NOT NULL AUTO_INCREMENT,
Nomcat varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Idcat, Nomcat)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE produits (
Codep bigint(21) NOT NULL AUTO_INCREMENT,
Reference bigint(21) NOT NULL,
Nomp varchar(20) COLLATE utf8_unicode_ci NOT NULL ,
Codef bigint(21) NOT NULL ,
Prenomf varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Idcat bigint(21) NOT NULL ,
Nomcat varchar(20) COLLATE utf8_unicode_ci NOT NULL,
Description varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (Codep ),
FOREIGN KEY (Codef, Prenomf) REFERENCES fournisseurs (Codef, Prenomf)
ON DELETE CASCADE
ON UPDATE CASCADE ,
FOREIGN KEY (Idcat, Nomcat) REFERENCES categorie (Idcat, Nomcat)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10;
我尝试插入Produits表,如下所示:
exports.ajouterprod = function(req, res) {
console.log("req", req.body);
var today = new Date();
var produits = {
"Reference": req.body.Reference,
"Nomp": req.body.Nomp,
// "Codef": req.body.Codef,
"Prenomf": req.body.Prenomf,
//"Idcat": req.body.Idcat,
"Nomcat": req.body.Nomcat,
"Description": req.body.Description
}
connection.query('INSERT INTO produits SET ?', produits, function(error, results, fields) {
if (error) {
console.log("error ocurred", error);
res.send({
"code": 400,
"failed": "error ocurred"
})
}
else {
res.send({
"code": 200,
"success": "produit registered sucessfully"
});
}
})
};
当我与Postman一起运行它时,我得到:“失败”:“出错”
出现错误{error:ER_NO_DEFAULT_FOR_FIELD:FIELD'Codef'在我的后端没有默认值。
如您所见,Codef
是我的表fourniseurs
上的主键
如何修复此问题?为什么会出现此错误:
由于您正在将数据插入到产品中,并且没有指定任何值Codef,因此会生成此错误,因为外键需要一个值(也可以为null)
解决方案1:
改变Produits的表结构,使其具有一些默认值,这些默认值存在于另一个表(引用外键的地方)中,或者为空值
解决方案2:
在代码级别添加一些默认值,并在引用外键的表中添加相同的值。谢谢@Shubham。我遵循解决方案2,效果很好。