我现在尝试用外键链接我的表,但是下面的表我有一些问题。
我的表格
app.controller('MainCtrl', function($scope) {
// need to define model in parent and pass to directive
$scope.locationText = {
value: ''
};
$scope.reset = function(){
$scope.locationText.value = '';
}
});
app.directive('smartGooglemaps', function() {
return {
restrict:'E',
replace:false,
// transclude:true,
scope: {
locationgoogle: '=',
locationText: '='
},
// ng-model="locationText.value"
template: '<input id="google_places_ac" placeholder="Please enter a location" name="google_places_ac" type="text" class="input-block-level" ng-model="locationText.value"/>',
link: function($scope, elm, attrs){
// implement directive googlemaps logic, set text value etc.
$scope.locationText.value = 'foo';
}
}
})
现在我想创建一个外键:
table users
- user_id INT(8) AUTO_INCREMENT //this is completly underlined
- username VARCHAR(150)
- password VARCHAR(50)
- auth_lvl VARCHAR(10) //this is "half underlined"
- ...
PRIMARY KEY: user_id
-------------------------------------------------
table groups
- group_id INT(11) AUTO_INCREMENT
- group_name VARCHAR(30)
- group_desc VARCHAR(60)
PRIMARY KEY: group_id
-------------------------------------------------
table users_groups
- user_id INT(10)
- group_id INT(10)
PRIMARY KEY: user_id, group_id (command was: PRIMARY KEY(user_id, group_id)
INDEX: group_id (these are seperated)
INDEX: user_id (these are seperated)
错误是:
ALTER TABLE `users_groups`
ADD FOREIGN KEY (`user_id`)
REFERENCES `DB`.`users` (`user_id`)
ON DELETE CASCADE
ON UPDATE RESTRICT
我想创建的第二个外键:
#1452 - Cannot add or update a child row: a foreign key constraint fails
(`db`.<result 2 when explaining filename '#sql-censored'>, CONSTRAINT
`#sql-censored` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
ON DELETE CASCADE)
错误是:
ALTER TABLE `users_groups`
ADD FOREIGN KEY (`group_id`)
REFERENCES `DB`.`groups` (`group_id`)
ON DELETE CASCADE
ON UPDATE RESTRICT
这些有什么问题?我找不到错误:/
答案 0 :(得分:0)
第一个似乎表明您的表中的数据不满足外键约束。 第二个可能是由不同的数据类型INT(10)与INT(11)引起的。 可能为所有列指定INTEGER将解决此问题。
答案 1 :(得分:0)
<强>解决方案强>
我在users_groups
中有一些行,其中user_id是用户的id,不再是users
表格中的。
我删除了users
中不存在的这些值后,一切正常!