MySQL 1215 Cannot add foreign key constraint - Laravel 5 - mysql

I'm stuck with this infamous error and can't figure out what went wrong. I'm trying to establish relationship between two tables orders and payments, whose migrations are defined as:
class CreateOrdersTable extends Migration
{
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('customer_id')->unsigned();
$table->integer('partner_id')->unsigned();
$table->string('status', 20)->default(Order::getDefaultStatus());
$table->string('paid', 20)->default('no');
$table->decimal('visitation_charges', 20, 2)->default(0);
$table->decimal('taxes', 20, 2)->default(0);
$table->decimal('charges', 20, 2)->default(0);
$table->decimal('discount', 20, 2)->default(0);
$table->decimal('total', 20, 2)->default(0);
$table->foreign('customer_id')->references('id')
->on('customers')->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('partner_id')->references('id')
->on('partners')->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down()
{
Schema::dropIfExists('orders');
}
}
class CreatePaymentsTable extends Migration
{
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('order_id')->unsigned();
$table->string('gateway', 100);
$table->string('transaction_id', 100);
$table->decimal('amount', 20, 2);
$table->string('status', 20)->default(Payment::getDefaultStatus());
$table->string('comments', 2000)->nullable();
$table->foreign('order_id')->references('id')
->on('orders')->onDelete('set null')
->onUpdate('cascade');
});
}
public function down()
{
Schema::dropIfExists('payments');
}
}
The error I get is:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `payments` add constraint `payments_order_id_foreign` foreign key (`order_id`) references `orders` (`id`) on delete set null on update cascade)
I've also verified that the table engines, column types, character set, etc., are the same (following are the outputs of show create):
| orders | CREATE TABLE `orders` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`customer_id` int(10) unsigned NOT NULL,
`partner_id` int(10) unsigned NOT NULL,
`status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'created',
`paid` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no',
`visitation_charges` decimal(20,2) NOT NULL DEFAULT '0.00',
`taxes` decimal(20,2) NOT NULL DEFAULT '0.00',
`charges` decimal(20,2) NOT NULL DEFAULT '0.00',
`discount` decimal(20,2) NOT NULL DEFAULT '0.00',
`total` decimal(20,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`),
KEY `orders_customer_id_foreign` (`customer_id`),
KEY `orders_partner_id_foreign` (`partner_id`),
CONSTRAINT `orders_customer_id_foreign` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `orders_partner_id_foreign` FOREIGN KEY (`partner_id`) REFERENCES `partners` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
| payments | CREATE TABLE `payments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`order_id` int(10) unsigned NOT NULL,
`gateway` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`transaction_id` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`amount` decimal(20,2) NOT NULL,
`status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'pending',
`comments` varchar(2000) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Not sure where I'm going wrong. :/

You can't make a foreign key ON DELETE SET NULL for columns declared as NOT NULL.
The column can't be NULL.
The foreign key works if you do either of the following:
Omit the ON DELETE SET NULL:
alter table `payments` add constraint `payments_order_id_foreign`
foreign key (`order_id`) references `orders` (`id`) on update cascade;
Modify the column to allow NULL:
alter table payments modify order_id int(10) unsigned null;

Add nullable() on order_id column. Like below. Your error will be gone.
class CreatePaymentsTable extends Migration
{
public function up()
{
Schema::create('payments', function (Blueprint $table) {
// your others columns
$table->integer('order_id')->unsigned()->nullable();
}
}

it should better you build another migration for foreign key:
php artisan make:migration add_foreign_key_to_payment_table --table=payments
so add payments foreign key to this migration, then run php artisan migrate
if the error still exists you must drop manually orders and payments table and remove them from migration table then add those again.
I hope this will help you. Good day! :)

Related

Duplicate key entry when updating object

I have a Product object that has multiple Shop objects because a shop can offer the same product at different prices / conditions.
I have an edit view for the products that lists the shops where the product is available.
When I make adjustments to the shops of the product eg. price; I get the error that the shop already exists in the database. I know the product exists, but I need the data to be updated.
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY'
public function update(Request $request, $slug)
{
$product = Product::with('shops', 'type')->where('slug', $slug)->first();
[... snip ...]
$i = 0;
foreach($product->shops as $shop) {
$shop = request('shop');
$product->shops()->attach($product->id, [
'shop_id' => $shop[$i]['id'],
'price' => $shop[$i]['price'],
'url' => $shop[$i]['url']
]);
$i++;
}
$product->save();
return redirect('/'.$slug)->with('success', 'Product has been updated');
}
$product->update(); yields the same result.
EDIT:
Product.php
class Product extends Model
{
protected $appends = ['lowest_price'];
public function shops(){
return $this->belongsToMany('App\Shop')->withPivot('price','url');
}
public function type(){
return $this->belongsTo('App\Type');
}
public function getLowestPriceAttribute()
{
$lowest_price = NULL;
foreach($this->shops as $shop) {
if(is_null($lowest_price)) {
$lowest_price = (double)$shop->pivot->price;
}
if($lowest_price > (double)$shop->pivot->price) {
$lowest_price = (double)$shop->pivot->price;
}
}
return $lowest_price;
}
}
Shop.php
class Shop extends Model
{
//
}
Shop migration
public function up()
{
Schema::create('shops', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('url');
$table->string('logo');
$table->timestamps();
});
[... snip ...]
}
EDIT2:
More info about the error:
Illuminate \ Database \ QueryException (23000)
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' (SQL: insert into `product_shop` (`price`, `product_id`, `shop_id`, `url`) values (500.00, 1, 1, http://test.com))
'CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`make` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`model` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`video` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`manufacturer_specs` text COLLATE utf8_unicode_ci NOT NULL,
`top_speed` decimal(8,1) NOT NULL,
`range` decimal(8,1) NOT NULL,
`weight` decimal(8,1) NOT NULL,
`type_id` int(10) unsigned NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`lowest_price` decimal(8,1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `products_slug_unique` (`slug`),
KEY `products_type_id_index` (`type_id`),
CONSTRAINT `products_type_id_foreign` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
'CREATE TABLE `product_shop` (
`product_id` int(10) unsigned NOT NULL,
`shop_id` int(10) unsigned NOT NULL,
`price` decimal(8,2) NOT NULL,
`url` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`product_id`,`shop_id`),
KEY `product_shop_product_id_index` (`product_id`),
KEY `product_shop_shop_id_index` (`shop_id`),
CONSTRAINT `product_shop_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE,
CONSTRAINT `product_shop_shop_id_foreign` FOREIGN KEY (`shop_id`) REFERENCES `shops` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
'CREATE TABLE `shops` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`logo` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
Edit3:
If I click the update button, I get the error even if I didn't change anything
You are trying to add another product-to-shop relation with the same keys, that's why you are seeing the index violation.
Instead of using attach, you can use sync:
$product->shops()->sync(
[
$shop[$i]['id'] => [
'price' => $shop[$i]['price'],
'url' => $shop[$i]['url']
]
], false);
The important part is the second parameter, which disabled detaching the other related items.
You could also use syncWithoutDetaching.
For details see:
Docs
Api

Mysql Laravel Constraint PRIMARY KEY MYSQL don't allowed

I have a simple table like that:
CREATE TABLE IF NOT EXISTS `dcp_type_discount` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`texte` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
And when i try to insert in another table a column foreign key to this table don't allow to me to do that.
In my local environement It is working.
But in my server i have an error like that
I am using laravel migration system and that is my migration :
public function up()
{
Schema::create('dcp_type_discount', function (Blueprint $table) {
$table->increments('id');
$table->string('code');
$table->string('texte');
$table->timestamps();
});
Schema::table('dcp_doc_item', function (Blueprint $table) {
$table->integer('type_remise_id')->nullable()->unsigned();
$table->foreign('type_remise_id')->references('id')->on('dcp_type_discount')->onDelete('cascade');
});
DB::table('dcp_type_discount')->insert(
[
[
'code' => '%',
'texte' => '%',
'created_at' => \Carbon\Carbon::now()->format('Y-m-d H:i:s')
],
[
'code' => 'UNIT',
'texte' => 'UNIT',
'created_at' => \Carbon\Carbon::now()->format('Y-m-d H:i:s')
]
]);
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('dcp_doc_item', function (Blueprint $table) {
$table->dropForeign(['type_remise_id']);
$table->dropColumn('type_remise_id');
});
Schema::drop('dcp_type_discount');
}
I think that the problem is not just because of laravel, because i tried in my PhpMyAdmin and when i try to add mannually an index on colonne type_remise_id, in the foreign column all primary key appear without this one.
I don't know what happen?!
Thanks to everybody.
PS: that is the decribe of my item table :
CREATE TABLE IF NOT EXISTS `dcp_doc_item` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`line` int(10) unsigned NOT NULL,
`material_id` int(10) unsigned NOT NULL,
`document_id` int(10) unsigned NOT NULL,
`quantity` double(8,2) NOT NULL,
`price_wo_vat` double(8,2) NOT NULL,
`vat` double(8,2) NOT NULL,
`discount` double(8,2) NOT NULL,
`net_price` double(8,2) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`price` double(8,2) DEFAULT NULL,
`qte_col` double(8,2) DEFAULT NULL,
`qte_colis` double(8,2) DEFAULT NULL,
`type_remise_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `dcp_doc_item_material_id_foreign` (`material_id`),
KEY `dcp_doc_item_document_id_foreign` (`document_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=85 ;

Foreign Key Constraint error for delete data form table

i am doing a project on ONLINE EXAMINATION which is copied from my senior but while execute i got some error. attach my code here and the error msg.
table #:
CREATE TABLE IF NOT EXISTS `question` (
`testid` bigint(20) NOT NULL DEFAULT '0',
`qnid` int(11) NOT NULL DEFAULT '0',
`question` varchar(500) DEFAULT NULL,
`optiona` varchar(100) DEFAULT NULL,
`optionb` varchar(100) DEFAULT NULL,
`optionc` varchar(100) DEFAULT NULL,
`optiond` varchar(100) DEFAULT NULL,
`correctanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
`marks` int(11) DEFAULT NULL,
PRIMARY KEY (`testid`,`qnid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table question
INSERT INTO `question` (`testid`, `qnid`, `question`, `optiona`, `optionb`, `optionc`, `optiond`, `correctanswer`, `marks`) VALUES
(1, 1, 'why use photoshop', 'image retouching', 'image making', 'image destroying', 'color coreection', 'optiona', 1),
(2, 1, 'java is', 'fish fry', 'software language', 'programing language', 'web maker', 'optionc', 1),
(2, 2, 'what is vaja', 'bengali', 'kokl', 'khsd', 'kojsgf', 'optiona', 1);
Table ###
--
-- Table structure for table student
CREATE TABLE IF NOT EXISTS `student` (
`stdid` bigint(20) NOT NULL,
`stdname` varchar(40) DEFAULT NULL,
`stdpassword` varchar(40) DEFAULT NULL,
`emailid` varchar(40) DEFAULT NULL,
`contactno` varchar(20) DEFAULT NULL,
`address` varchar(40) DEFAULT NULL,
`city` varchar(40) DEFAULT NULL,
`pincode` varchar(20) DEFAULT NULL,
PRIMARY KEY (`stdid`),
UNIQUE KEY `stdname` (`stdname`),
UNIQUE KEY `emailid` (`emailid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table studentquestion
CREATE TABLE IF NOT EXISTS `studentquestion` (
`stdid` bigint(20) NOT NULL DEFAULT '0',
`testid` bigint(20) NOT NULL DEFAULT '0',
`qnid` int(11) NOT NULL DEFAULT '0',
`answered` enum('answered','unanswered','review') DEFAULT NULL,
`stdanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
PRIMARY KEY (`stdid`,`testid`,`qnid`),
KEY `testid` (`testid`,`qnid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Constraints for table `question`
--
ALTER TABLE `question`
ADD CONSTRAINT `question_ibfk_1` FOREIGN KEY (`testid`) REFERENCES `test` (`testid`);
--
-- Constraints for table `studentquestion`
--
ALTER TABLE `studentquestion`
ADD CONSTRAINT `studentquestion_ibfk_1` FOREIGN KEY (`stdid`) REFERENCES `student` (`stdid`),
ADD CONSTRAINT `studentquestion_ibfk_2` FOREIGN KEY (`testid`, `qnid`) REFERENCES `question` (`testid`, `qnid`);
ERROR MESSAGE
Cannot delete or update a parent row: a foreign key constraint fails (oes.studentquestion, CONSTRAINT studentquestion_ibfk_2 FOREIGN KEY (testid, qnid) REFERENCES question (testid, qnid))
Perhaps it is not the full script you have.
Most probably the data is inserted in wrong order. The error occurs because the table studentquestion already has the data for the testid and qnid fields which is not present is the question master table. You have to reorder your insert-statements so that master tables are populated first and the detail tables after them.

Foreign key constraint fails even if parent row is present

I have following 2 tables.
CREATE TABLE `RuleEntity` (
`rule_id` bigint(20) NOT NULL,
`action_word` varchar(255) DEFAULT NULL,
`condition_word` varchar(255) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`ruleGUID` varchar(255) NOT NULL,
`rule_type` varchar(255) DEFAULT NULL,
`caseflag` tinyint(1) NOT NULL DEFAULT '0',
`globalflag` tinyint(1) NOT NULL DEFAULT '1',
`multilineflag` tinyint(1) NOT NULL DEFAULT '1',
`isDefault` tinyint(1) DEFAULT '0',
PRIMARY KEY (`rule_id`),
UNIQUE KEY `ruleGUID` (`ruleGUID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `PreProcessorRule` (
`id` bigint(20) NOT NULL,
`rank` int(11) DEFAULT NULL,
`rule_rule_id` bigint(20) DEFAULT NULL,
`defaultRuleGUID` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK4BD420EB4273A655` (`rule_rule_id`),
CONSTRAINT `FK4BD420EB4273A655` FOREIGN KEY (`rule_rule_id`) REFERENCES `RuleEntity` (`rule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I am trying to insert 1 row in RuleEntiy and another in PreprocessorRule. But it fails with foreign key constraint.
INSERT INTO `RuleEntity` (`rule_id`,`action_word`,`condition_word`,`description`,`caseflag`,`globalflag`,`multilineflag`,`name`,`ruleGUID`,`rule_type`, `isDefault`) VALUES ( '1',' ','[^\\x00-\\x7F]','HF Default Rule: Removes Non-ASCII Character.',1,1,1,'HF: Non-ASCII Remover','1157B568665E08','CUSTOM', 1);
Query OK, 1 row affected (0.02 sec)
INSERT INTO `HFP`.`PreProcessorRule` (`id`, `rank`, `rule_rule_id`) VALUES ('1', '0', '1');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HFP`.`PreProcessorRule`, CONSTRAINT `FK4BD420EB4273A655` FOREIGN KEY (`rule_rule_id`) REFERENCES `RuleEntity` (`rule_id`))
I ran same sql on local MySQL 5.1 on MAC OS X 10.9. It worked. Now I am trying to run it on the Amazon RDS MySQL where I get this error.
Kindly let me know what is going wrong?

Error 1452 Foreign Key Fails

I am getting a 1452 error from MySQL. Here is the SQL I used to INSERT
INSERT INTO `Quote` (`QTE_id`, `USR_id`, `QTE_total`, `QTE_desc`, `QTE_dateCreated`, `QTE_dateModified`,`QTE_name`, `QTE_status`)
VALUES
(39, 2, NULL, 'desc', '2013-11-19 00:00:00', '2013-11-19 11:22:49', 'test', 'Active');
Cannot add or update a child row: a foreign key constraint fails (`dbNAME`.`Quote`, CONSTRAINT `USR_id1` FOREIGN KEY (`USR_id`) REFERENCES `users` (`USR_id`))
I am positive that the USR_id I am trying to put into the Quote table exists. Any ideas? A lot of the other questions on Stack Overflow did not answer my question.
Here is the Create syntax for the following tables I am trying to insert and relate (generated from Workbench):
CREATE TABLE `Quote` (
`QTE_id` int(11) NOT NULL AUTO_INCREMENT,
`USR_id` int(11) DEFAULT NULL,
`QTE_total` decimal(7,2) DEFAULT NULL,
`QTE_desc` text,
`QTE_dateCreated` timestamp NULL DEFAULT NULL,
`QTE_dateModified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`QTE_name` varchar(255) DEFAULT NULL,
`QTE_status` varchar(30) DEFAULT NULL,
PRIMARY KEY (`QTE_id`),
KEY `USR_id1` (`USR_id`),
CONSTRAINT `USR_id1` FOREIGN KEY (`USR_id`) REFERENCES `users` (`USR_id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;
And the User Table:
CREATE TABLE `Users` (
`USR_id` int(11) NOT NULL AUTO_INCREMENT,
`MGR_id` int(11) NOT NULL DEFAULT '0'
`REP_id` int(11) NOT NULL,
`USR_name` varchar(255) NOT NULL,
`USR_login` varchar(255) NOT NULL,
`USR_dateModified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`USR_dateCreated` datetime NOT NULL,
`USR_role` enum('Salesperson','Manager') DEFAULT NULL,
PRIMARY KEY (`USR_id`,`MGR_id`,`REP_id`),
KEY `MGR_id_idx` (`MGR_id`),
KEY `REP_id_idx` (`REP_id`),
KEY `USR_login` (`USR_login`),
CONSTRAINT `MGR_id` FOREIGN KEY (`MGR_id`) REFERENCES `users` (`USR_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `REP_id` FOREIGN KEY (`REP_id`) REFERENCES `representative` (`REP_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
I had to do a few small changes (change the users to Users, add a ',' at the end of line that defines MGR_id, remove the constraint for REP_id) to make the two CREATEs to work.
Here are the exact CREATEs:
CREATE TABLE `Users` (
`USR_id` int(11) NOT NULL AUTO_INCREMENT,
`MGR_id` int(11) NOT NULL DEFAULT '0',
`REP_id` int(11) NOT NULL,
`USR_name` varchar(255) NOT NULL,
`USR_login` varchar(255) NOT NULL,
`USR_dateModified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`USR_dateCreated` datetime NOT NULL,
`USR_role` enum('Salesperson','Manager') DEFAULT NULL,
PRIMARY KEY (`USR_id`,`MGR_id`,`REP_id`),
KEY `MGR_id_idx` (`MGR_id`),
KEY `REP_id_idx` (`REP_id`),
KEY `USR_login` (`USR_login`),
CONSTRAINT `MGR_id` FOREIGN KEY (`MGR_id`) REFERENCES `Users` (`USR_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `Quote` (
`QTE_id` int(11) NOT NULL AUTO_INCREMENT,
`USR_id` int(11) DEFAULT NULL,
`QTE_total` decimal(7,2) DEFAULT NULL,
`QTE_desc` text,
`QTE_dateCreated` timestamp NULL DEFAULT NULL,
`QTE_dateModified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`QTE_name` varchar(255) DEFAULT NULL,
`QTE_status` varchar(30) DEFAULT NULL,
PRIMARY KEY (`QTE_id`),
KEY `USR_id1` (`USR_id`),
CONSTRAINT `USR_id1` FOREIGN KEY (`USR_id`) REFERENCES `Users` (`USR_id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;
With that in place the following two INSERTs worked:
mysql> INSERT INTO `Users` (`USR_id`, `MGR_id`) VALUES(2, 2);
Query OK, 1 row affected, 4 warnings (0.22 sec)
mysql> INSERT INTO `Quote` (`QTE_id`, `USR_id`, `QTE_total`, `QTE_desc`, `QTE_dateCreated`, `QTE_dateModified`,`QTE_name`, `QTE_status`) VALUES (39, 2, NULL, 'desc', '2013-11-19 00:00:00', '2013-11-19 11:22:49', 'test', 'Active');
Query OK, 1 row affected (0.22 sec)
I hope this helps.