Laravel How to define a composite primary key in the model.

Source: Internet
Author: User
The structure of the table is as follows:

REATE TABLE `carts` (  `user_id` int(10) unsigned NOT NULL,  `product_id` char(64) NOT NULL,  `quantity` int(10) unsigned NOT NULL,  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  PRIMARY KEY (`user_id`,`product_id`),  KEY `fk_idx_cart_product` (`product_id`),  CONSTRAINT `fk_idx_cart_product` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`),  CONSTRAINT `fk_idx_cart_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8

Need to use eloquent ORM's Save () method, he will save the data according to PrimaryKey, how to set this compound primary key in model?

/** * The primary key for the model. * * @var string */protected $primaryKey = 'id';
$cart = Cart::firstOrNew([...]);$cart->quantity = $quantity;$cart->save();

SQLSTATE[42S22]: Column not found:1054 Unknown Column ' id ' in ' WHERE clause '

Reply content:

The structure of the table is as follows:

REATE TABLE `carts` (  `user_id` int(10) unsigned NOT NULL,  `product_id` char(64) NOT NULL,  `quantity` int(10) unsigned NOT NULL,  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  PRIMARY KEY (`user_id`,`product_id`),  KEY `fk_idx_cart_product` (`product_id`),  CONSTRAINT `fk_idx_cart_product` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`),  CONSTRAINT `fk_idx_cart_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8

Need to use eloquent ORM's Save () method, he will save the data according to PrimaryKey, how to set this compound primary key in model?

/** * The primary key for the model. * * @var string */protected $primaryKey = 'id';
$cart = Cart::firstOrNew([...]);$cart->quantity = $quantity;$cart->save();

SQLSTATE[42S22]: Column not found:1054 Unknown Column ' id ' in ' WHERE clause '

This problem is not resolved, most of the other framework is not resolved, so there is no way to define a composite primary key in the eloquent ORM, the solution:
1. Use doctrine.
2. Validation in form validation can also achieve the effect of primary key constraints.
On this issue in fact, a year ago, a foreign programmer on GitHub issue, and Taylorotwell also participated in the discussion, he gave the reply is laravel currently no plan to increase this feature. This feature was not present until today (2015.11.07). Please see the following link for details: https://github.com/laravel/framework/issues/5355

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.