Foreign key relationship with composite primary keys in SQL Server 2005

Since Table2 has a composite primary key (FileID, FileType), then any reference to it must also include both columns. ALTER TABLE dbo.Table1 ADD CONSTRAINT FK_Table1_Table2 FOREIGN KEY(FileID, FileType) REFERENCES Table2(FileID, FileType) Unless you have a unique constraint/index on the Table2.FileID field (but if so: why isn’t this the PK??), you cannot create a FK relationship … Read more

Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?

Let me answer step by step. 1. When do you need ` insertable = false, updatable = false`? Let’s look at the below mapping, public class Zip { @ManyToOne @JoinColumn(name = “country_code”, referencedColumnName = “iso_code”) private Country country = null @Column(name = “country_code”) private String countryCode; } Here we are referring to the same column … Read more

Laravel Model with Two Primary Keys update [duplicate]

I’ve run into this problem a couple of times. You need to override some properties: protected $primaryKey = [‘user_id’, ‘stock_id’]; public $incrementing = false; and methods (credit): /** * Set the keys for a save update query. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ protected function setKeysForSaveQuery(Builder $query) { $keys = $this->getKeyName(); if(!is_array($keys)){ … Read more

How to create and handle composite primary key in JPA

You can make an Embedded class, which contains your two keys, and then have a reference to that class as EmbeddedId in your Entity. You would need the @EmbeddedId and @Embeddable annotations. @Entity public class YourEntity { @EmbeddedId private MyKey myKey; @Column(name = “ColumnA”) private String columnA; /** Your getters and setters **/ } @Embeddable … Read more