방금 해 봤는데 복합키는 잘 됩니다. 다만 Model::find(), Model::first(), 다른 모델간의 관계를 연결할 때 외래키/참조키 연결은 좀 더 조사해 보시면 좋겠습니다. 다 방법이 있을 겁니다.
~ $ laravel new composite-key ~ $ vim .env # .env DB_CONNECTION=sqlite ~/composite-keys $ cd composite-keys && touch database/database.sqlite ~/composite-keys $ php artisan make:model Device --migration # Model created successfully. # Created Migration: 2017_01_10_122827_create_devices_table ~/composite-keys $ vim 2017_01_10_122827_create_devices_table.php <?php // create_devices_table.php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateDevicesTable extends Migration { public function up() { Schema::create('devices', function (Blueprint $table) { $table->string('phonenumber'); $table->uuid('uuid'); $table->timestamps(); $table->primary(['phonenumber', 'uuid']); }); } public function down() { Schema::dropIfExists('devices'); } } ~/composite-keys $ php artisan tinker >>> App\Device::unguard(); => null >>> App\Device::create(['phonenumber' => '010-1234-5678', 'uuid' => '12345678-1234-5678-9012-123456789012']); => App\Device {#679 phonenumber: "010-1234-5678", uuid: "12345678-1234-5678-9012-123456789012", updated_at: "2017-01-10 12:35:21", created_at: "2017-01-10 12:35:21", id: 1, } >>> App\Device::create(['phonenumber' => '010-1234-5678', 'uuid' => '12345678-1234-5678-9012-123456789012']); Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: devices.phonenumber, devices.uuid (SQL: insert into "devices" ("phonenumber", "uuid", "updated_at", "created_at") values (010-1234-5678, 12345678-1234-5678-9012-123456789012, 2017-01-10 12:35:26, 2017-01-10 12:35:26))' >>> App\Device::create(['phonenumber' => '010-1234-5678', 'uuid' => '87654321-1234-5678-9012-123456789012']); => App\Device {#681 phonenumber: "010-1234-5678", uuid: "87654321-1234-5678-9012-123456789012", updated_at: "2017-01-10 12:35:52", created_at: "2017-01-10 12:35:52", id: 2, }
질문하신게 복합 기본 키 (Composite Primary Key) 맞죠? 우선 https://laravel.kr/docs/5.3/migrations#indexes 에서,
$table->primary(['first', 'last']);
확인해시고요. 저도 테스트 프로젝트 만들어서 실험해 볼게요.
아~~ 넹넹~~ 아~ 지원 안한댓는데 제가 소스를 좀더 뒤져볼껄 그랬었나 봅니다 ;;;
ㅠ_ㅠ 감사합니다~~
주원님 말씀하신 대로 공식 문서에 $primaryKey 에 배열을 넣도록 적혀는 있는데 제대로 지원은 안되는 거 같에요 ;;;
Illumiate/Database/Eloquent/Model.php
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Model.php#L1079
/**
* Get the primary key for the model.
*
* @return string
*/
public function getKeyName()
{
return $this->primaryKey;
}
/**
* Get the primary key value for a save query.
*
* @return mixed
*/
protected function getKeyForSaveQuery()
{
return isset($this->original[$this->getKeyName()])
? $this->original[$this->getKeyName()]
: $this->getAttribute($this->getKeyName());
}
getKeyForSaveQuery 부분에서 getKeyName()을 부르면 설정한 $primaryKey값을 불러오는데 항상 string 단일값이 돌아오는 것으로 내부 코드가 짜여져 있더라구요.... ;
프로젝트 하면서 필요해서 복합키 Model::delete 를 호출했는데 에러가 나길래 살펴봤었는데 지금은 단일키로 롤백했습니다 ㅠㅠ
문제 1.
Multiple Connection 상황에서 Eloquent ORM 에서 쿼리로그 확인하는 방법좀 알려주세요
위 문제는 아직도 답보 상태이고...
여전히 해결할 기미도 보이지 않는 가운데...
( 지원을 안하는 것 같은 느낌적인 느낌이 듭니다. )
문제2.
Table에 조합 primaryKey 가 존재하는 경우
예를 들자면
Mysql 기준
PRIMARY KEY (userid , servicetype),
요론 식으로
PK가 생성되었을 경우에 문제를 해결해보려고 이것저것 기웃거리다가...
I currently have no plans to implement composite key support.
ㅠ_ㅠ
On Thursday, June 25, 2015, Jack notifications@github.com wrote:
라는 좌절스러운 이야기를 봤습니다.
물론 2년전 이야기지만...
아직 까지 지원을 안하는것 같은 기분이 듭니다.
(기분 탓이었으면 좋겠습니다.)
혹시 2번 문제는 해결이 되었을까요??? ㅜ_ㅜ