foreign-keys

Join table twice - on two different columns of the same table

巧了我就是萌 提交于 2019-11-29 16:51:00
问题 I have a very confusing database with a table that holds two values I need in a separate table. Here is my issue: Table1 - id Table2 - id - table1_id - table3_id_1 - table3_id_2 Table3 - id - value I need to go from table1 and do a join that would give me back the value from table3 in two separate columns. So I want something like this: table1.id | table2.id | table2.table3_id_1 | table2.table3_id_2 | X | Y Where X and Y are the values for the row connected by table3_id_1 and table3_id_2

Django models: mutual references between two classes and impossibility to use forward declaration in python

为君一笑 提交于 2019-11-29 16:47:29
问题 I have defined two models where each one references the other, like so: class User(models.Model): # ... loves = models.ManyToManyField(Article, related_name='loved_by') class Article(models.Model): # ... author = models.ForeignKey(User) You see, the problem is both classes references each other. No matter in what order these two classes are implemented, python always raises NameError exception, complaining either one class is not defined. 回答1: You can find the solution in the docs: If you

Foreign keys and NULL in mySQL

你。 提交于 2019-11-29 16:47:17
问题 Can I have a column in my values table (value) referenced as a foreign key to knownValues table, and let it be NULL whenever needed, like in the example: Table: values product type value freevalue 0 1 NULL 100 1 2 NULL 25 3 3 1 NULL Table: types id name prefix 0 length cm 1 weight kg 2 fruit NULL Table: knownValues id Type name 0 2 banana Note: The types in the table values & knownValues are of course referenced into the types table. 回答1: NULLs in foreign keys are perfectly acceptable.

Multiple relationships between two entities, is this good practice?

Deadly 提交于 2019-11-29 16:46:48
I have the following typical scenario regarding an office and its staff: Each staff member belongs to one office Each office has only one manager (a staff member) ER Model As you can see, this results in the relationship being recorded twice, once for the foreign key in the office table to point to the manager, and also in the staff table pointing to the office that staff member works for. I have looked into alternative ways of modelling this but am still a bit lost. Please could someone advice a suitable way of modelling this, or if my method is acceptable for the scenario. Many thanks It's

TSQL foreign keys on views?

天涯浪子 提交于 2019-11-29 16:31:25
问题 I have a SQL-Server 2008 database and a schema which uses foreign key constraints to enforce referential integrity. Works as intended. Now the user creates views on the original tables to work on subsets of the data only. My problem is that filtering certain datasets in some tables but not in others will violate the foreign key constraints. Imagine two tables "one" and "two". "one" contains just an id column with values 1,2,3. "Two" references "one". Now you create views on both tables. The

Identifying Sybase tables, fields, keys, constraints

◇◆丶佛笑我妖孽 提交于 2019-11-29 16:19:35
I'm trying to set up a Sybase query that will give me the following output: Table KeyType KeyNumber Column table1 PK 1 table1_id table1 FK 2 table2_id table1 FK 3 table3_id table1 FK 4 table4_id table1 Unique 5 table1_abc table1 Unique 5 table1_def In other words, I need the PK for each table, and every foreign key it has, as well as every unique key (not where a key has more than one element, such as the unique key above, this is identified by having the same KeyNumber). I'm guessing I need to use sysobject, syscolumns, syskeys and sysconstraints but I can't seem to figure out how they

CASCADE Delete in many-to-many self-reference table

梦想的初衷 提交于 2019-11-29 15:13:25
Table DISPLAY_TAB below is a self-reference table that can contain both parent and child tabs. A parent tab can have multiple child tabs and a child tab can belong to multiple parents. I'd like to establish a CASCADE DELETE relationship between main table and relationship table DISPLAY_TAB_GROUPING so when either parent or child tab is deleted - relationship is automatically deleted as well (just relationship, not actual tab record). So I am creating a FOREIGN KEY constrain on DISPLAY_TAB_GROUPING for fields TAB_ID_R_1 and TAB_ID_R_2 tables, referencing TAB_ID in DISPLAY_TAB table. And it

Can a database attribute be primary and foreign key?

假如想象 提交于 2019-11-29 14:55:21
问题 I have 2 tables, User and Employee . Each user is given a User_ID and that is a primary key in the User table and a foreign key in the Employee table. Can that attribute in the Employee table also be a primary key? 回答1: If you have a one-to-one relation between two tables, then the primary key of the details table is a foreign key as well. master detail (1 : 1) +----------+ 1:1 +-------------+ | PK id |<---o| PK FK id | +----------+ +-------------+ | col1 | | col1 | | col2 | | col2 | | etc. |

Rails custom foreign_key name on both table

徘徊边缘 提交于 2019-11-29 14:54:59
问题 I have two models, for example User and Club with their attributes: User: id uid email etc. and Club: id player_id address supporter etc. For some reason, the join attribute is clubs.player_id with users.uid NOT clubs.player_id with users.id . Is it possible connecting these two model with one-to-one association using has_one and belongs_to ? thx 回答1: I bet this would work: class User < ActiveRecord::Base has_one :club, :foreign_key => :player_id, :primary_key => :uid end class Club <

What creates the FOREIGN KEY constraint in Ruby on Rails 3?

天大地大妈咪最大 提交于 2019-11-29 14:29:47
问题 I understand that by default the id field is created and also: PRIMARY KEY ( id ) . How about the foreign key ? I have Shops and Products tables and the following associations: Shop: has_many :products Product: belongs_to :shop In Product I have also: t.integer "shop_id" which is meant to be the foreign key, and also: add_index("products", "shop_id") However, if I export the database I see only: KEY `index_products_on_shop_id` (`shop_id`) What should I do in order to add FOREIGN KEY (`shop_id