foreign-keys

Hibernate one-to-one (on foreign key) vs one-to-one (on primary key)

☆樱花仙子☆ 提交于 2019-12-12 18:34:47
问题 I have questions about What are the benefits of using a one-to-one association on foreign key or a one-to-one association on the primary key ? I've read the documentation of Hibernate available at: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html#assoc-unidirectional-121 Can someone tell me with details the plus of using the first or the second implementation? Another question, I have a one-to-one unidirectional relationship between User and Perimeter. (User ---->

mssql table multi foreign key cascade

こ雲淡風輕ζ 提交于 2019-12-12 16:02:15
问题 I'm confident that this is possible but for the life of me I can't figure it out. What I have created is a user history MSSQL table to hold the changes made to a user and by whom. This table contains two foreign keys which reference my other table (User) - one fkey for the affected user and the other fkey for the user making the changes. What I need is for any changes to the (User) table to cascade and update the corresponding entries in this new table. The fields in the new table (User

Is there such a thing as a reverse foreign key?

亡梦爱人 提交于 2019-12-12 14:08:11
问题 In MySQL, is there a way to specify that a.column cannot exist in b.column - a reverse foreign key? In other words: # Would not return any rows ever SELECT * FROM a INNER JOIN b ON a.column = b.column; # Would fail INSERT INTO a SELECT * FROM b; # Would not insert any rows INSERT IGNORE INTO a SELECT * FROM b; 回答1: No there is no such thing. You would need to do that in a trigger: DELIMITER $$ CREATE TRIGGER bi_a_each BEFORE INSERT ON a FOR EACH ROW BEGIN DECLARE forbidden_key INTEGER; SELECT

Postgresql foreign key — no unique constraint

天涯浪子 提交于 2019-12-12 12:08:49
问题 I have the following case: I have a table that contains text entries in various languages. It is defined as follows: CREATE TABLE text ( textid character varying(70) NOT NULL, language character varying(10) NOT NULL, content text NOT NULL, CONSTRAINT text_pk PRIMARY KEY (textid , language ), CONSTRAINT languages_text_fk FOREIGN KEY (language) REFERENCES languages (language) MATCH SIMPLE ON UPDATE CASCADE ON DELETE RESTRICT ); the languages table is just a list of language codes and it is not

Conditional Foreign Key to multiple tables

回眸只為那壹抹淺笑 提交于 2019-12-12 12:02:27
问题 I have a table which contains two type of data, either for Company or Employee. Identifying that data by either 'C' or 'E' & a column storing primary key of it. So how can I give foreign key depending on data contained & maintain referential integrity dynamically. id | referenceid | documenttype ------------------------------- 1 | 12 | E 2 | 7 | C Now row with id 1 should reference Employee table with pk 12 & row with id 2 should reference Company table with pk 7. Otherwise I have to make two

“Integrity constraint violation: 1062 Duplicate entry” - but no duplicate rows

牧云@^-^@ 提交于 2019-12-12 11:34:40
问题 I'm converting an app from native mysqli calls to PDO. Running into an error when attempting to insert a row into a table with a foreign key constraint. Note: this is a simplified test case and should not be copy/pasted into a production environment. Info PHP 5.3, MySQL 5.4 First, here are the tables: CREATE TABLE `z_one` ( `customer_id` int(10) unsigned NOT NULL DEFAULT '0', `name_last` varchar(255) DEFAULT NULL, `name_first` varchar(255) DEFAULT NULL, `dateadded` datetime DEFAULT NULL,

Entity Framework 5: Code-First Cyclical Relationship Issues

天涯浪子 提交于 2019-12-12 11:23:56
问题 I understand why EF does not allow "cyclical references" in the PK/FK relationships. I am looking for advice on how to alter my model to make the following scenario work. Scenario Three entities: Employee , Agency , WorkRecord . Their purpose is to log Employee time spent doing work. Employee then contains reference to the Agency he/she is employed by, and his/her WorkRecord contain reference to the Agency the work was done for. public class Employee { [Key] public int Id { get; set; } public

Delete all records that have no foreign key constraints

蓝咒 提交于 2019-12-12 10:53:28
问题 I have a SQL 2005 table with millions of rows in it that is being hit by users all day and night. This table is referenced by 20 or so other tables that have foreign key constraints. What I am needing to do on a regular basis is delete all records from this table where the "Active" field is set to false AND there are no other records in any of the child tables that reference the parent record. What is the most efficient way of doing this short of trying to delete each one at a time and

Django: fill model's fields with values from another model (at init and save)

为君一笑 提交于 2019-12-12 10:23:08
问题 I've got 2 models (for the example). class A(models.Model): name = models.CharField() class B(models.Model): a = models.ForeignKey(A) my_name = models.CharField() So, I want to create (and update) the field my_name of the instance of B, with the field name of the instance of A to which it's related (one to many relation). I've tried: class B(models.Model): .... def __init__(self, *args, **kwargs): self.my_name = self.a.name But I've got the error: AttributeError Exception Value: 'B' object

Table-agnostic Foreign Keys?

怎甘沉沦 提交于 2019-12-12 09:47:27
问题 First, I did read this StackOverflow question, so no need to point me towards it. I'm working on a similar problem right now. Specifically, I have a database with an Auditing table that is used to store auditing info about other tables within the db. The basic form of this table is: ID, EntityID, EntityTypeID, ActionTypeID, DateTime Now, as you can guess, the generic nature of EntityID means that foreign key relationships to and from this table are tricky to manage, especially with you