foreign-key-relationship

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

Entity Framework (Database-First) multiple relations to same table naming conventions control

≯℡__Kan透↙ 提交于 2019-12-04 00:00:39
Let's suppose that we have this situation: Tables in database: Country (id, country_name), Person (id, login), CountryManager (id_country, id_person), CountryStakeholder (id_country, id_person) If we had to create the model from the database, using Entity Framework Database-First, in VS we'd have a class like this: class Country { int id; string country_name; virtual ICollection<Person> Person1; // Navigation Properties virtual ICollection<Person> Person2; // ---------||---------- } I've simplified the code a lot, but hopefully you got the point. Seems that when Entity Framework deals with

SQL 2000: T-SQL to get foreign key relationships for a table

*爱你&永不变心* 提交于 2019-12-03 16:43:34
Similar but NOT IDENTICAL to SQL Server 2000 - Query a Table’s Foreign Key relationships I need a T-SQL statement that will work SQL 2000 that given a table name, will return the foreign key relationships for that table e.g. Table MyFristTable has a foreign key to MySecondTable, where MyFirstTable.ColA must be in MySecondTable.ColB. I'd be delighted, if the sql statement (or stored proc) is ran for MyFirstTable and returned a result set on the lines of Column | FK_Table | FK_COLUMN ---------------------------------- ColA | MySecondTable | ColB NB : I have samples for SQL 2005 that won't work

Laravel 4 and Eloquent: retrieving all records and all related records

南笙酒味 提交于 2019-12-03 08:58:57
I have two classes: Artist and Instrument . Each Artist can play one or more Instrument s. And each Instrument can be assigned to one or more Artist s. So, I've set up the following Classes: Artist.php public function instruments() { return $this->belongsToMany('App\Models\Instrument'); } Instrument.php public function artists() { return $this->belongsToMany('\App\Models\Artist'); } Then I have three database tables: artists: id, firstname, lastname, (timestamps) instruments: id, name artist_instrument: id, artist_id, instrument_id I'm able to successfully retrieve a one artist and their

How does use_for_related_fields work in Django?

懵懂的女人 提交于 2019-12-03 08:51:19
问题 I'm unable to grasp that from the docs. It's totally unclear to me, more specifically: Is it a global setting? So if I specify this attribute it on one of the model managers, will it be used globally by all the model classes? If it's not a global setting then which relationships exactly will be affected? Is it possible to have one model manager for one relationship and another one for another relationship with the same model? Most of all I would appreciate any good minimal example usages as

SQL mapping between multiple tables

梦想的初衷 提交于 2019-12-03 08:41:00
This is a SQL design question. First, the setup. I have three tables: A, which is automatically populated based on a query against a linked server. The data in this table cannot be changed; B, which has just a dozen or so rows, containing the names for collections of As; AtoB, which is the mapping table by which As are organized into named collections, with foreign keys on both columns; For example, A contains: Giraffe Owl Tiger And B contains: Seattle Zoo San Jose Zoo And AtoB contains: 1,1 (Giraffe in Seattle) 2,1 (Owl in Seattle) 3,1 (Tiger in Seattle) 2,2 (Owl in San Jose) Now, the problem

MySQL - Using foreign key as primary key too

偶尔善良 提交于 2019-12-03 01:11:15
I have table 1 with a primary key user_id and table 2 where user_id is a foreign key. Only 1 record per user_id can exist in table 2, and no record can exist without it. QUESTION: Can user_id in table 2 be both foreign and primary key at the same time, and if yes, is it a good idea, what are pros/cons? Yes, you can do this (and you should, from a database design point of view). However, consider what it means if user_id is the primary key on table 2. You are in effect saying that each row in table 2 corresponds to a user, but you already have a table where each row corresponds to a user: table

Need to set a 1 to many relationship in the same table in Laravel 4

ぐ巨炮叔叔 提交于 2019-12-03 00:30:49
I have the following models Category: <?php class Category extends Eloquent { protected $table = "category"; protected $fillable = array('title','parent','metatit','metadsc','metake','metaurl','image'); public function categoryitems(){ return $this->hasMany('CategoryItem','catid'); } public function parent(){ return $this->hasMany('category','parent'); } public function child(){ return $this->belongsTo('Category','parent'); } } Need to set a 1 to many relationship in the category table Ex category "cities" is a child of category "countries" the error happen when i try to use the following code

How does use_for_related_fields work in Django?

眉间皱痕 提交于 2019-12-02 22:44:01
I'm unable to grasp that from the docs. It's totally unclear to me, more specifically: Is it a global setting? So if I specify this attribute it on one of the model managers, will it be used globally by all the model classes? If it's not a global setting then which relationships exactly will be affected? Is it possible to have one model manager for one relationship and another one for another relationship with the same model? Most of all I would appreciate any good minimal example usages as the documentation lacks those afaik. Thanks. Is it a global setting? So if I specify this attribute it

Cannot delete or update a parent row ConstraintViolationException

南笙酒味 提交于 2019-12-02 21:09:26
问题 I have 2 object entities (User and Phone) and they are supposed to have many-to-many relations. User.java //all columns @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) @JoinTable(name = "USER_PHONE", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "phone_id", referencedColumnName = "id")) private List<Phone> phones; Phone.java //all columns @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)