I am trying to insert values into my comments table and I am getting a error. Its saying that I can not add or update child row and I have no idea what that means. My schema looks something like this:
--
-- Baza danych: `koxu1996_test`
--
-- --------------------------------------------------------
--
-- Struktura tabeli dla tabeli `user`
--
CREATE TABLE IF NOT EXISTS `user` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`username` varchar(32) COLLATE utf8_bin NOT NULL,
`password` varchar(64) COLLATE utf8_bin NOT NULL,
`password_real` char(32) COLLATE utf8_bin NOT NULL,
`email` varchar(32) COLLATE utf8_bin NOT NULL,
`code` char(8) COLLATE utf8_bin NOT NULL,
`activated` enum('0','1') COLLATE utf8_bin NOT NULL DEFAULT '0',
`activation_key` char(32) COLLATE utf8_bin NOT NULL,
`reset_key` varchar(32) COLLATE utf8_bin NOT NULL,
`name` varchar(32) COLLATE utf8_bin NOT NULL,
`street` varchar(32) COLLATE utf8_bin NOT NULL,
`house_number` varchar(32) COLLATE utf8_bin NOT NULL,
`apartment_number` varchar(32) COLLATE utf8_bin NOT NULL,
`city` varchar(32) COLLATE utf8_bin NOT NULL,
`zip_code` varchar(32) COLLATE utf8_bin NOT NULL,
`phone_number` varchar(16) COLLATE utf8_bin NOT NULL,
`country` int(8) NOT NULL,
`province` int(8) NOT NULL,
`pesel` varchar(32) COLLATE utf8_bin NOT NULL,
`register_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`authorised_time` datetime NOT NULL,
`edit_time` datetime NOT NULL,
`saldo` decimal(9,2) NOT NULL,
`referer_id` int(8) NOT NULL,
`level` int(8) NOT NULL,
PRIMARY KEY (`id`),
KEY `country` (`country`),
KEY `province` (`province`),
KEY `referer_id` (`referer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=83 ;
and the mysql statement I am trying to do looks something like this:
INSERT INTO `user` (`password`, `code`, `activation_key`, `reset_key`, `register_time`, `edit_time`, `saldo`, `referer_id`, `level`) VALUES (:yp0, :yp1, :yp2, :yp3, NOW(), NOW(), :yp4, :yp5, :yp6). Bound with :yp0='fa1269ea0d8c8723b5734305e48f7d46', :yp1='F154', :yp2='adc53c85bb2982e4b719470d3c247973', :yp3='', :yp4='0', :yp5=0, :yp6=1
the error I get looks like this:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
koxu1996_test
.user
, CONSTRAINTuser_ibfk_1
FOREIGN KEY (country
) REFERENCEScountry_type
(id
) ON DELETE NO ACTION ON UPDATE NO ACTION)
It just simply means that the value for column country on table comments you are inserting doesn't exist on table country_type or you are not inserting value for country on table user . Bear in mind that the values of column country on table comments is dependent on the values of ID on table country_type .
You have foreign keys between this table and another table and that new row would violate that constraint.
You should be able to see the constraint if you run show create table user
, it shows up as CONSTRAINT...
and it shows what columns reference what tables/columns.
In this case country
references country_type (id)
and you are not specifying the value of country
. You need to put a value that exists in country_type
.
Just to throw in my own issue in case it can help someone else, I was copy/pasting entries in my migration files and messed up by putting quotes around an integer. Since the value I was trying to enter was considered a string going into an integer field that was referencing another integer, this error came up.
Another option could be thath your primary key in source table IS NOT unsigned, so I solved same insert with (notice id
int(8) unsigned):
CREATE TABLE IF NOT EXISTS
user
(id
int(8) unsigned NOT NULL AUTO_INCREMENT,username
varchar(32) COLLATE utf8_bin NOT NULL,
password
varchar(64) COLLATE utf8_bin NOT NULL,password_real
char(32) COLLATE utf8_bin NOT NULL,code
char(8) COLLATE utf8_bin NOT NULL,
activated
enum('0','1') COLLATE utf8_bin NOT NULL DEFAULT '0',
activation_key
char(32) COLLATE utf8_bin NOT NULL,reset_key
varchar(32) COLLATE utf8_bin NOT NULL,name
varchar(32) COLLATE utf8_bin NOT NULL,street
varchar(32) COLLATE utf8_bin NOT NULL,
house_number
varchar(32) COLLATE utf8_bin NOT NULL,
apartment_number
varchar(32) COLLATE utf8_bin NOT NULL,city
varchar(32) COLLATE utf8_bin NOT NULL,zip_code
varchar(32) COLLATE utf8_bin NOT NULL,phone_number
varchar(16) COLLATE utf8_bin NOT NULL,country
int(8) NOT NULL,province
int(8) NOT NULL,pesel
varchar(32) COLLATE utf8_bin NOT NULL,
register_time
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
authorised_time
datetime NOT NULL,edit_time
datetime NOT NULL,saldo
decimal(9,2) NOT NULL,referer_id
int(8) NOT NULL,
level
int(8) NOT NULL, PRIMARY KEY (id
), KEYcountry
(country
), KEYprovince
(province
), KEYreferer_id
(referer_id
) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=83 ;
In my case, the value was empty for target table column while the reference table column has it. hence was throwing this error.
In my case the references values does not corresponding on the related table. Just be sure the values exist on reference table and currents rows has corresponding valid values.
In my case , its no problem in SQL commands , but the inputs was not sending as $request , so in php.ini file :
max_input_vars was 1000 by default and I changed it to :
max_input_vars = 2000
then you have to restart web server .
You should pass NULL
for empty values. Not empty string such as ''
in my case this was the error: Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'brand_name_ur' cannot be null (SQL: insert into brands
( brand_name_en
, brand_name_ur
, brand_slug_en
, brand_slug_ur
, brand_image
) values (SAMI, ?, sami, سامی, upload/brand/1742772943246486.png))
and fixed by
'brand_name_ur' => $request -> brand_image_ur, < changing brand_image_ur to brand_name_ur. Noob mistak. took my hour.
thanks to @Tsimtsum
By running a request like:
SELECT user.id as user_id, country as user_country_type_id, country_type.id as country_type_id
FROM `user`
LEFT JOIN country_type ON country_type.id = user.country
WHERE country_type.id is null;
it probably returns some rows and it should not !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.