Eloquent should support composite keys
Eloquent assumes heavily that you are using surrogate keys on your tables (the id field in every table). However in real world usage, particularly older database systems not designed with web frameworks in mind, there are often tables with composite keys. That is, the primary key is over two or more columns. Often, you can't just add autoincremented id's. This is definitely a game stopper for a lot of use-cases, where you are forced to access existing RDBMS (e.g. building a frontend for an existing db). See also this reddit post about this issue.
The schema builder has already support for composite keys, however eloquent doesn't seem to support it yet.
I request the feature to use composite keys in your eloquent models and queries. The default configuration for surrogate keys is fine, just add the option.
I would like to have this supported too.
Me too.
I agree, this is extremely annoying at the moment. Unfortunately I'm not in charge of a lot of data I need to access.
For the moment, I'm just overriding newQuery() in my models by doing something like
function newQuery()
{
$query = parent::newQuery();
$query->... // Add constraints here
return $query;
}
@nesl247 this is a good workaround. So did you specify primaryKey with one field and added the second field as a where-clause in the newQuery method? Can you give me some actual code?
You could view my bug report (#5517) which has a work-around that works flawlessly for me.
@aebersold Yes, that is exactly what I did. In my case I was using a BaseModel and a bunch of child classes that extended my BaseModel, so I had a protected $secondaryKey = 'something' and a protected $type = 'someType'.
function newQuery()
{
$query = parent::newQuery();
$query->where($this->secondaryKey, '=', $this->type);
return $query;
}
I would really like to have this supported as well.
+1
I put this in my Eloquent Model subclass to enforce my extra column ('locale'). Maybe it'll help someone.
protected function setKeysForSaveQuery(Builder $query)
{
parent::setKeysForSaveQuery($query);
$query->where('locale', '=', $this->locale);
return $query;
}
+1
I just get an common error while saving model with composite keys
PDO::lastInsertId() expects parameter 1 to be string, array given
+1
I want it!
-1 This is not "should" stuff. Please keep Eloquent simple. As you mentioned, query builder supported it, so it is enough.
Don't do every thing by Eloquent. Just use query builder, results became collections in L5, so what problem?
I doubt this will ever happen.
Why do you doubt this will ever happen?
Composite keys are extremely common, if the SchemaBuilder supports it why wouldn't you want to have it supported by Eloquent as well?
Sometimes using surrogate keys are wasteful, especially if you're using UUIDs as identifiers... having a useless column you never search on that just takes up table & index space is silly sometimes.
Keeping Eloquent "simple" is a very silly reason. To the end-user nothing changes - but they now have the ability to specify an array for the primary key. If Laravel/Eloquent really expects to be taken seriously and provide further credit for using PHP for web development it should naturally support what every other ORM supports for alternative web development libraries.
Using pivot only is doable when you have a few extra columns on your relation... otherwise it's very awkward.
Using the query builder for saving & deleting is kind of silly, why would anyone want those inconsistencies in their code? Some places you use Eloquent for saving/deleting and some places you use QueryBuilder? This kind of attitude is poor and sounds lazy.
If Laravel is open to receiving a feature like this, I would be more than willing to supply a nice simple and efficient solution.
+1
+1 Maybe in 90% of my projects I use composite primary keys. For localisation and for one to many relationships composite keys is the best practice for me... I'm very dissapointed that in Laravel 5 there is no composite keys, but a lot of complicated design patterns. That is my opinion. Don't want to argue with anyone.
Eloquent follows the Active Record convention, so it needs a single primary key to work. If you need composite keys in your project, maybe Eloquent isn't the best tool for it.
The Active Record pattern makes no restrictions about primary keys - I don't know where you're getting that from.
Eloquent/Laravel is currently developed to work with a single primary key, there's no reason why someone shouldn't be able to make a contribution to the project which adds composite keys. What is the best way to go about this?
All I'm saying is that this is a convention in other frameworks as well. Since Rails made it popular, the other frameworks followed the same path, that's what we see out there with most PHP frameworks (except Yii 2, for what I know).
Don't get me wrong, I would love to see Eloquent working with composite keys but I agree with Campbell, we're not going to see that happen so soon and my opinion is that I don't think it's worth the trouble. Well, only Taylor can tell :)