foreign-keys

DELETE all where MySQL foreign key constraint does not fail

余生长醉 提交于 2019-12-04 02:41:23
I am trying to delete a few records but am getting the following error: Cannot delete or update a parent row: a foreign key constraint fails The thing is, the foreign key constraint is failing for only 1 or 2 of my 100 records I wish to delete. I wish to write a query which deletes these 98-99 records, skipping the 1 or 2 which failed , which I can later manually inspect and delete/modify. Not stopping because of some single problematic record, but continuing with the others, ignoring that. Is there a neat way to do this ? You have to LEFT JOIN the referencing table and add a condition saying

EF4: Get the linked column names from NavigationProperty of an EDMX

戏子无情 提交于 2019-12-04 02:39:51
I am generating POCOs (lets say they are subclasses of MyEntityObject ) by using a T4 template from an EDMX file. I have 3 entities, e.g.: MyTable1 (PrimaryKey: MyTable1ID) MyTable2 (PrimaryKey: MyTable2ID) MyTable3 (PrimaryKey: MyTable3ID) These entities have the following relations: MyTable1.MyTable1ID <=> MyTable2.MyTable1ID (MyTable1ID is the foreign key to MyTable1) MyTable2.MyTable2ID <=> MyTable3.MyTable2ID (MyTable2ID is the foreign key to MyTable2) Or in another view: MyTable1 <= MyTable2 <= MyTable3 I want to extract all foreign key relations NavigationProperty[] foreignKeys = entity

MySQL Foreign Key, Can't create table (errno: 150)

杀马特。学长 韩版系。学妹 提交于 2019-12-04 02:12:32
I am trying to build the database and tables for my system. But I found that if I don't add the foreign key in the codes. There is no error. I've used many method try to make the codes works, but it still have error. I am using MySQL 5.5.31, and the codes here: CREATE DATABASE TOS; DROP TABLE TOS.USER CASCADE; DROP TABLE TOS.BILL_HEADER CASCADE; DROP TABLE TOS.TOY CASCADE; CREATE TABLE TOS.USER (User Char(8), Name Char(10), Type Char(1), Password Char(12), PRIMARY KEY(User)); CREATE TABLE TOS.BILL_HEADER (Bill_No Char(10), CTime DateTime, No_Of INTEGER, Cus_No Char(5), DTime DateTime, PRIMARY

Mysql foreign key by non unique key — how is that possible?

戏子无情 提交于 2019-12-04 01:13:30
问题 I was migrating mysql database to postgres and stumbled across the following block in DDL (Note: This is what I got from mysqldump): CREATE TABLE `catalog_property_value` ( `id` int(10) unsigned NOT NULL, `property_id` int(10) unsigned NOT NULL, `sort` int(10) unsigned NOT NULL, `value_number` decimal(15,5) DEFAULT NULL, `value_string` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`,`sort`), KEY `FK_catalog_property_value` (`property_id`), KEY `NewIndex1` (`id`), CONSTRAINT `FK_catalog_property

Foreign key optimization in SQLite

旧街凉风 提交于 2019-12-04 00:22:56
I know that SQLite does not enforce foreign keys natively, but that's not my primary concern. The question is: If I declare CREATE TABLE invoice ( invoiceID INTEGER PRIMARY KEY, clientID INTEGER REFERENCES client(clientID), ... ) will sqlite at least use the information that clientID is a foreign key to optimize queries and automatically index invoice.clientID, or is this constraint a real no-op? Even if it is not actually a no-op (a data structure describing the constraint is added to the table), foreign key related statement doesn't create any index on involved columns. Indexes are

What Does Rails Do With Both :dependent => :destroy and cascade delete/nullify/restrict

狂风中的少年 提交于 2019-12-04 00:22:22
I'm trying to decide how best to set up (if at all) foreign key constraints for my rails application. I have a model Response that belongs_to a Prompt . I would like to use :dependent => :destroy to have destroy called on every Response that belongs to a deleted Prompt and I'm trying to decide what delete constraint I should place on my foreign key. In short I want advice about how I can get best take advantage of both the destroy method on dependent objects and foreign key constraints to ensure cruft doesn't accumulate and reflect the logical structure of the data being stored. Several

How to implement ONE-TO-MANY in a database

僤鯓⒐⒋嵵緔 提交于 2019-12-03 23:54:18
I want to implement one-to-many concept in my application. This is the scenario: I have two tables (i). Person(ID, NAME, AGE, DEPT) (ii). Person Responsibilities(ID, RESP'S) One person may have more than one responsibility. How shall I implement a 1-n relationship here? Actually, I don't understand the correct concepts for this. Any suggestions or links to understand this concept would be appreciated. This one-to-many relationship can be interpreted in plain English like this... A Person has one or more responsibilities, AND Each responsibility belongs to exactly one person. Now depending on

Django: list_filter and foreign key fields

梦想的初衷 提交于 2019-12-03 22:44:15
Django doesn't support getting foreign key values from list_display or list_filter (e.g foo__bar). I know you can create a module method as a workaround for list_display, but how would I go about to do the same for list_filter? Thanks. Django supports list_filter with foreign key fields # models.py: class Foo(models.Model): name = models.CharField(max_length=255) def __unicode__(self): return self.name class Bar(models.Model): name = models.CharField(max_length=255) foo = models.ForeignKey(Foo) # admin.py: class BarAdmin(admin.ModelAdmin): list_filter = ('foo__name') From documentation: Field

How to alter “REFERENCES” in PostgreSQL?

为君一笑 提交于 2019-12-03 22:38:29
How can I alter the reference to a table in PostgreSQL when the table name has been changed? Say I have: CREATE TABLE example1 ( id serial NOT NULL PRIMARY KEY, name varchar(100) ); CREATE TABLE example2 ( id serial NOT NULL PRIMARY KEY, example1fk integer REFERENCES example1 (id) DEFERRABLE INITIALLY DEFERRED ); Later I do: ALTER TABLE example1 RENAME TO example3; How to change the definition of the foreign key constraint? example1fk integer REFERENCES example1 (id) DEFERRABLE INITIALLY DEFERRED, Internal dependencies between tables and / or other objects are never bound to the object name.

How to relate 3 tables depending on event

◇◆丶佛笑我妖孽 提交于 2019-12-03 22:30:23
问题 I have a table that have information about different types of events that can be done by persons in two categories civil and worker so for each one of them I have their respective tables civil{ civil_id, name, age,telephone...} the primary key is civil_id worker{ worker_id, name, duty, department...} the primary key is worker_id then the event table has a list of all possible events event {type_of_event} the primary key is type_of_event then I am planing to store information in other table