foreign-keys

MySQL “set unique_checks”, “set foreign_key_checks” vs. “alter table disable keys”

匆匆过客 提交于 2019-12-22 06:50:22
问题 We're having a problem where a mysqldump script is spending 90% of it's time populating a small handful of the tables it deals with. Eliminating FK's and indexes eliminates the speed problem, but is not an acceptable solution. The dump script does have: /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; Can we expect any different behavior from ALTER TABLE foo DISABLE KEYS ? Also, is disable

How to populate a table's foreign keys from other tables

夙愿已清 提交于 2019-12-22 06:47:14
问题 I've got the following tables, of which translation is empty and I'm trying to fill: translation { id translated language_id template_id } language { id langname langcode } template { id tplname source domain total } The source data to fill translation is a temporary table that I've populated from an external CSV file: tmp_table { id translated langname tplname source domain } What I'd like to do is to fill translation with the values from tmp_table . The translated field can be copied

Override joined-inheritance foreign key name with JPA/Hibernate

徘徊边缘 提交于 2019-12-22 06:09:09
问题 I have a class CD with inherit from class Media: CD: @Entity public class CD extends Media { ... } Media: @Entity(name = "media") @Inheritance(strategy = InheritanceType.JOINED) public abstract class Media extends PersistenceId<Long> { ... } JPA generates automatically a foreign key name and I would like to override this with the name I want: 03-10 18:16:58.174 [main] DEBUG org.hibernate.SQL - alter table cd add constraint FK_ehd468g2cptgh6bq6sxe75xlf foreign key (id) references media (id)

Hibernate second level cache and ON DELETE CASCADE in database schema

ぐ巨炮叔叔 提交于 2019-12-22 05:23:19
问题 Our Java application has about 100 classes mapped to a database (SQL Server or MySQL). We are using Hibernate as our ORM (with XML mapping files). We specify FOREIGN KEY constraints in our database schema. Most of our FOREIGN KEY constraints also specify ON DELETE CASCADE . We've recently started enabling Hibernate 2nd level caching (for popular entities and collections) to mitigate some performance problems. Performance has improved since we enabled the 2nd level cache. However we've also

MySQL: Insert if foreign key exists

血红的双手。 提交于 2019-12-22 05:07:14
问题 I have an Excel sheet with around 2.000 rows that I want to insert into my database. The problem is that the table I want to insert the 2.000 rows into has a column that references to a foreign key in another table. Unfortunately a lot of queries fail, since the provided foreign key value does NOT exist. I know that I can ignore foreign key checks, but this is not what I want. I don't want to ignore foreign key checks, I just want bad queries not to be executed. Example: INSERT INTO test (id,

MySQL: Insert if foreign key exists

▼魔方 西西 提交于 2019-12-22 05:06:08
问题 I have an Excel sheet with around 2.000 rows that I want to insert into my database. The problem is that the table I want to insert the 2.000 rows into has a column that references to a foreign key in another table. Unfortunately a lot of queries fail, since the provided foreign key value does NOT exist. I know that I can ignore foreign key checks, but this is not what I want. I don't want to ignore foreign key checks, I just want bad queries not to be executed. Example: INSERT INTO test (id,

Grails - multiple belongsTo of same class with cascading deletion

▼魔方 西西 提交于 2019-12-22 04:36:17
问题 This one is for the Grails users here. I asked it on the grails - user mailing list, but I figured since I've been fighting this for a couple of days I should cast as wide a net as possible. I'm having some difficulty with trying to model relationships between two objects of the same type in another object (different type) referencing the two objects. As an example of what I'm trying to do, suppose you're modeling relationships among family members. Any given relationship "belongsTo" two

DJANGO: How to list_display a reverse foreign key attribute?

大兔子大兔子 提交于 2019-12-22 04:20:46
问题 I'm building a web app that tracks what library books a person checks out. I have the following models: class Person(models.Model): name = models.CharField(max_length=100) def __unicode__(self): return self.name class Book(models.Model): name = models.CharField(max_length=100) person = models.ForeignKey(Person) checkout_date = models.DateTimeField('checkout date') def __unicode__(self): return self.name On the Admin's "change list" page for Person, I would like to show what books that person

How to find if a referenced object can be deleted?

纵然是瞬间 提交于 2019-12-22 04:07:46
问题 I have an object called "Customer" which will be used in the other tables as foreign keys. The problem is that I want to know if a "Customer" can be deleted (ie, it is not being referenced in any other tables). Is this possible with Nhibernate? 回答1: What you are asking is to find the existence of the Customer PK value in the referenced tables FK column. There are many ways you can go about this: as kgiannakakis noted, try to do the delete and if an exception is thrown rollback. Effective but

“polymorphism” for FOREIGN KEY constraints

妖精的绣舞 提交于 2019-12-22 03:48:09
问题 There is this field in a table: room_id INT NOT NULL CONSTRAINT room_id_ref_room REFERENCES room I have three 2 tables for two kinds of rooms: standard_room and family_room How to do something like this: room_id INT NOT NULL CONSTRAINT room_id_ref_room REFERENCES standard_room or family_room I mean, room_id should reference either standard_room or family_room . Is it possible to do so? 回答1: Here is the pattern I've been using. CREATE TABLE room ( room_id serial primary key, room_type VARCHAR