This question is completely different from similar ones. There is no foreign key in the SQL query. This is a silly error I see when I import the SQL file on remote server. This is the SQL code
CREATE TABLE `locations` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
As you see there is no foreign key, But when I run the following code, it is ok
CREATE TABLE `locations` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ;
If I rename it to something else it is OK too.
CREATE TABLE `locationssss` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
what is wrong?
Just for future references:
Do you have more tables within your database? If so, is there a table that does contain a foreign key connected with the locations table?
I am creating a web portal for an organization and I am a bit confused on this part.
They will be receiving donations from their registered members as well as guests. I was thinking of creating a users table that is solely used for registered members and no guests etc. because users table will contain unique "email" column and I don't want it to be null.
For donations, I can add user_id foreign key for users table.
What I am thinking of doing is that I should add "name" and "mobile" columns in donations table, so that if it's a guest, we should only get his name and phone number and put in donations table. Do you think this is the right way?
For just demo purpose I am showing you the table:
users table
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`mobile` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`),
UNIQUE KEY `users_mobile_unique` (`mobile`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
donations table
CREATE TABLE IF NOT EXISTS `donations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`mobile` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `users_user_id_foreign` (`user_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Any help would be much appreciated.
Thanks
There are many ways to solve data problems - some better than others. I would not have a separate user table if you are already getting some details. Rather have a REGISTERED_USER column or along those lines to denote a "full user" versus a partial. Then everything stays relatively simple AND the user has an option to become a full user later, which I assume you want for donations... :)
I do not know how to handle my issue here and I am looking for the most practical solution. I have to track a user-id across multiple tables and I am looking at triggers and Foreign Keys to solve the initial inserts into the databases for each user. On registration, I would like to create a row in each database with the user id. That way its always an update statement when I reference it in the code.
Here is my Table Structure
CREATE TABLE `authentication` (
`userid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fname` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`lname` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`online` int(11) DEFAULT NULL,
`created_at` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`email`),
KEY `userid` (`userid`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `user_progress` (
`userid` int(10) NOT NULL,
`progress_event` varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Here is an example trigger I tried to make
CREATE TRIGGER user_progress_add
AFTER INSERT ON authentication
FOR EACH ROW
BEGIN
INSERT INTO user_progress (userid, progress_status)
VALUES (NEW.userid, 'signup');
END
This trigger did not work. It gives the error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1".
I guess my question is, whats wrong with my trigger? is a trigger a better idea in this case than a foreign key?
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, CONSTRAINT user_ibfk_1 FOREIGN KEY
(country) REFERENCES country_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, 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 ;
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 !
You can delete it (probably a bad idea if you are in production) using request like :
DELETE user
FROM `user`
LEFT JOIN country_type ON country_type.id = user.country
WHERE country_type.id is null;
and adding constraint should be done after !
add to your migrations
$this->addSql('SET foreign_key_checks = 0');
.....
$this->addSql('SET foreign_key_checks = 1');
I ran this sql query in my database:
update payments set method = 'paysafecard' AND amount = 25 WHERE payment_id IN (1,2,3,4,5,...)
Of course i meant set method = 'paysafecard' , amount = 25
However I did it in phpmyadmin and it showed me that rows were affected. After running it again it showed 0 rows affected.
I don't know what may have changed in the database, what could this have done?
My table looks like this:
CREATE TABLE IF NOT EXISTS `payments` (
`payment_id` int(11) NOT NULL AUTO_INCREMENT,
`method_unique_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`method` enum('moneybookers','paypal','admin','wallet','voucher','sofortueberweisung','bitcoin','paysafecard','paymentwall') COLLATE utf8_unicode_ci NOT NULL,
`method_tid` int(11) DEFAULT NULL,
`uid` int(11) NOT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`plan` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`expires_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`amount` decimal(8,2) NOT NULL,
`currency` enum('EUR','USD','BTC') COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`payment_id`),
UNIQUE KEY `method` (`method`,`method_tid`),
UNIQUE KEY `method_unique_id` (`method_unique_id`,`method`),
KEY `expires_at` (`expires_at`),
KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=8030 ;
I am running
-- Server version: 5.1.41
-- PHP Version: 5.3.2-1ubuntu4.11
This would result in the method field being set to '0' for all of your records fitting the where clause.
It is interpreted as the following:
set method = ('paysafecard' AND amount = 25)
This is a logical AND, and results in a boolean value for these records(which will be parsed to the corresponding field of your column).