composite-primary-key

Only one key from composite primary key as foreign key

余生长醉 提交于 2019-12-04 19:20:18
In this database, key1 & key2 make up the composite primary key of table4 , but i'm able to add a foreign key to table3 that comprise just key1 . Why MySQL allows this? Does the above database design make no sense at all? TL;DR It's not standard SQL. MySQL documentation itself advises not doing it. In SQL a FOREIGN KEY declaration must reference a column list declared PRIMARY KEY or UNIQUE NOT NULL. (PRIMARY KEY creates a UNIQUE NOT NULL constraint.) (The constraint has to be explicit, even though any set of NOT NULL columns containing a set that is UNIQUE has to be unique.) The MySQL

Laravel Eloquent CANNOT Save Models with Composite Primary Keys

大憨熊 提交于 2019-12-04 04:21:03
问题 When defining composite primary keys then calling save on an instanced model, an exception is thrown. ErrorException (E_UNKNOWN) PDO::lastInsertId() expects parameter 1 to be string, array given The error occurred at line 32 $id = $query->getConnection()->getPdo()->lastInsertId($sequence); And here's the declaration of Model class ActivityAttendance extends Eloquent { /** * The database table used by the model. * * @var string */ protected $table = 'activity_attendance'; /** * The primary key

Hibernate foreign key with a part of composite primary key

谁说我不能喝 提交于 2019-12-04 03:36:39
I have to work with Hibernate and I am not very sure how to solve this problem, I have 2 tables with a 1..n relationship like this: ------- TABLE_A ------- col_b (pk) col_c (pk) [other fields] ------- TABLE_B ------- col_a (pk) col_b (pk) (fk TABLE_A.col_b) col_c (fk TABLE_A.col_c) [other fields] How can I manage this with Hibernate? I do not have any idea how to declare a foreign key that would contain a part of primary key. My database schema is generated from the Hibernate model. I have found two solutions to this problem. The first one is rather a workaround and is not so neat as the

Change Primary Key to Composite Key (Primary Key already exists)

淺唱寂寞╮ 提交于 2019-12-04 00:21:03
问题 I am trying to change the primary key of a table in my SQL database from the existing key to a composite key, which does not include the existing column. The following code is not working due to the following error messages: DROP PRIMARY KEY: Incorrect Syntax near PRIMARY. Expecting COLUMN, CONSTRAINT, ID, or QUOTED_ID ADD PRIMARY KEY: Incorrect Syntax near PRIMARY. Expecting ID T-SQL code: ALTER TABLE AgentIdentification DROP PRIMARY KEY Number, ADD PRIMARY KEY (AgentId, IdIndicator) EDIT I

Composite Key in JPA / Hibernate with inherited class

不羁岁月 提交于 2019-12-03 10:03:13
问题 i have a composite id defined on my class structure as below. Unfortunatly i always get a hibernate error that complains on the not found "part2": "Property of @IdClass not found in entity MoreClass : part2" Can anybody help me on fixing the problem? (or at least point me on a helpful jpa/hibernate doc?) @IdClass(ClassKey.class) @Entity public class MoreClass extends LessClass implements Serializable { @Id String part1; } @MappedSuperclass public class LessClass implements Serializable { @Id

how to make two column as a primary key in hibernate annotation class

我们两清 提交于 2019-12-03 06:19:08
问题 This is my annotation class and i want userId and groupId column both as primary key. I have found more questions (Question) about this, but didn't found relevant answer. I have less reputation, so I am not able to comment on posts, So I am putting my question here. This is my code.. import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id

Define a unique primary key based on 2 columns

泄露秘密 提交于 2019-12-03 04:14:12
问题 I would like to define a unique key for records based on 2 columns : 'id' and 'language' to let the user submits the following strings : id=1 language=en value=blabla english id=1 language=fr value=blabla french I tried to use set_primary_key and also add_index but it didn't work ( add_index :words, ["id", "language_id"], :unique => true ) I have the following model : class Word < ActiveRecord::Base belongs_to :dictionnary belongs_to :language attr_accessible :lang, :rev, :value, :dictionnary

Composite Key in JPA / Hibernate with inherited class

末鹿安然 提交于 2019-12-03 00:35:44
i have a composite id defined on my class structure as below. Unfortunatly i always get a hibernate error that complains on the not found "part2": "Property of @IdClass not found in entity MoreClass : part2" Can anybody help me on fixing the problem? (or at least point me on a helpful jpa/hibernate doc?) @IdClass(ClassKey.class) @Entity public class MoreClass extends LessClass implements Serializable { @Id String part1; } @MappedSuperclass public class LessClass implements Serializable { @Id String part2; } public class ClassKey implements Serializable { String part1; String part2; } Michael

how to make two column as a primary key in hibernate annotation class

笑着哭i 提交于 2019-12-02 19:43:22
This is my annotation class and i want userId and groupId column both as primary key. I have found more questions ( Question ) about this, but didn't found relevant answer. I have less reputation, so I am not able to comment on posts, So I am putting my question here. This is my code.. import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.NaturalId; @Entity @Table(name=

Limitation of JPA 1.0 using @IdClass with *nested* composite primary keys?

无人久伴 提交于 2019-12-02 06:45:48
问题 Given the following example (departments - projects): A department has the following properties (composite primary key): @Entity @IdClass(DeptId.class) public class Department { @Id @Column(name="number") private Integer number; @Id @Column(name="country") private String country; @Column(name="name") private String name; @OneToMany(mappedBy="dept") private Collection<Project> projects; ... } Here the PK class: public class DeptId implements Serializable { private Integer number; private