framework icon indicating copy to clipboard operation
framework copied to clipboard

Eloquent should support composite keys

Open aebersold opened this issue 12 years ago • 84 comments

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.

aebersold avatar Aug 06 '14 09:08 aebersold

I would like to have this supported too.

Marwelln avatar Aug 06 '14 18:08 Marwelln

Me too.

fabioboris avatar Aug 07 '14 01:08 fabioboris

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 avatar Aug 21 '14 20:08 nesl247

@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?

aebersold avatar Aug 21 '14 21:08 aebersold

You could view my bug report (#5517) which has a work-around that works flawlessly for me.

tsilenzio avatar Aug 25 '14 13:08 tsilenzio

@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;
}

nesl247 avatar Aug 26 '14 02:08 nesl247

I would really like to have this supported as well.

pmccarren avatar Nov 05 '14 15:11 pmccarren

+1

cgwyllie avatar Nov 18 '14 17:11 cgwyllie

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;
    }

weotch avatar Nov 19 '14 00:11 weotch

+1 I just get an common error while saving model with composite keys PDO::lastInsertId() expects parameter 1 to be string, array given

ghost avatar Dec 17 '14 14:12 ghost

+1

jonagoldman avatar Dec 18 '14 16:12 jonagoldman

I want it!

piotrchludzinski avatar Jan 29 '15 16:01 piotrchludzinski

-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?

HiroKws avatar Jan 30 '15 06:01 HiroKws

I doubt this will ever happen.

GrahamCampbell avatar Jan 30 '15 20:01 GrahamCampbell

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.

ClickerMonkey avatar Feb 21 '15 00:02 ClickerMonkey

+1

Evertt avatar Feb 23 '15 12:02 Evertt

+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.

lotestudio avatar Mar 03 '15 19:03 lotestudio

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.

vvinhas avatar Mar 03 '15 21:03 vvinhas

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?

ClickerMonkey avatar Mar 03 '15 21:03 ClickerMonkey

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 :)

vvinhas avatar Mar 03 '15 21:03 vvinhas