foreign-key-relationship

How to make two foreign keys to same model unique together?

寵の児 提交于 2019-12-04 19:09:33
Let's say I have a relationship class such as: class Friendship(models.Model): person1 = models.ForeignKey(Person, related_name='person1') person2 = models.ForeignKey(Person, related_name='person2') so I want to make this object unique for a pair of Person s. If I simply do unique_together = (("person1", "person2"),) then I can end up with two Friendship objects where FS1.person1 = A, FS1.person2 = B FS2.person1 = B, FS2.person2 = A I do not want this. I want a unique friendship object between two people. So how can I ensure that there is -at most- one Friendship object for any pair of Person

Entity Framework - Reverse Foreign Key Check

风格不统一 提交于 2019-12-04 18:50:20
We have a system that allows Administrators to build out new content types within the system, including foreign key linkages to other tables. The Admin can then rebuild the database, at which point it creates the tables and all the necessary relationships, then rebuilds the EDMX and recompiles everything. Works like a champ (I didn't write it or I might know the answer to this). One drawback that we have is when a user goes to delete a record that may be linked to by an item in another table. This throws an error due to referential integrity. I'm trapping this, of course, but all I can provide

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

半城伤御伤魂 提交于 2019-12-04 14:24:30
问题 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

SQL mapping between multiple tables

北慕城南 提交于 2019-12-04 13:18:23
问题 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

How to find all foreign keys?

回眸只為那壹抹淺笑 提交于 2019-12-04 13:13:14
I'd like to find all referencing tables in my db that have a foreign key that points to a specific referenced table. Is there a query that I can run to do this? Not sure if the question is confusing. Let me know if it is and I can try to explain it in more detail. The following query or Modification tehreof will do - in Sql server You can also supply catalog and schema info select tab1.TABLE_NAME from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as ref inner join INFORMATION_SCHEMA.TABLE_CONSTRAINTS as prim on ref.UNIQUE_CONSTRAINT_NAME=prim.CONSTRAINT_NAME and ref.UNIQUE_CONSTRAINT_CATALOG=prim

Disable creating nested objects in django rest framework

本秂侑毒 提交于 2019-12-04 12:49:22
问题 Hi, Can you help me how to disable creating nested objects ? I have serializers like this: (Employee has ForeignKey to Team) class TeamSerializer(serializers.ModelSerializer): class Meta: model = Team fields = ('id', 'name') class EmployeeSerializer(serializers.ModelSerializer): class Meta: model = Employee fields = ('id', 'name', 'surname', 'team') depth = 1 or instead could be: class EmployeeSerializer(serializers.ModelSerializer): team = TeamSerializer() class Meta: model = Employee fields

Should a two-to-many data relationship be treated as many-to-many?

 ̄綄美尐妖づ 提交于 2019-12-04 12:36:51
I have 2 database tables: Teams and Games. For the purpose of this question, we are dealing with football (soccer) teams and games. Each Game has exactly 2 teams, generally a home team and an away team although occasionally both teams can be neutral. My question is whether I should represent this data relationship using 2 foreign keys in the Games table (home_team_id, away_team_id) or whether I should use a many-to-many relationship with a games_teams table to link the two, in which case I would need to also store whether the team was the home or away team and seems a little overkill. To add

postgresql foreign key syntax

眉间皱痕 提交于 2019-12-04 07:23:00
问题 I have 2 tables as you will see in my posgresql code below. The first table students has 2 columns, one for student_name and the other student_id which is the primary key. In my second table called tests, this has 4 columns, one for subject_id, one for the subject_name, then one for a student with the higest score in a subject which is highestStudent_id. am trying to make highestStudent_id refer to student_id in my students table. This is the code i have below , am not sure if the syntax is

Maintaining multiple one-to-many

巧了我就是萌 提交于 2019-12-04 06:42:46
问题 Following on from NHibernate one-to-one vs 2 many-to-one Is there an easy way to maintain multiple one-to-many relationships which are being used as a pseudo one-to-one. E.g. If I have 2 entities, User and Contact, which are related by a FK on each (User.ContactId and Contact.UserID). What is the best way to maintain that each reference points at the other. It would be wrong for the system to update User with a different contact, but the Contact still references User... 回答1: Most likely you

mysql export sql dump alphabatically,which cause foreign key constraints error during import

☆樱花仙子☆ 提交于 2019-12-04 03:30:21
I have 10 tables in my database(MySQL). two of them is given below tbl_state state_id |int(10) |UNSIGNED ZEROFILL auto_increment state_name |varchar(40) tbl_city city_id |int(10) |UNSIGNED ZEROFILL auto_increment city_name |varchar(40) | state_code |int(10) | UNSIGNED ZEROFILL (FK reference with tbl_state.state_id) Foreign Key Constraint : tbl_city.state_code is references to tbl_state.state_id . now my problem is when I export all tables and import again then it gives foreign key constraint fails error.... because when I export mysql dump, sql dump is generated in alphabetically ordered