foreign-keys

Is it possible to reference one column as multiple foreign keys?

有些话、适合烂在心里 提交于 2019-11-27 11:42:14
I have few tables, and I want to reference one column from PDF table to multiple other tables. for example if PDF table select output looks like this: ITEM_TYPE ITEM_ID QUANTITY 1 23 3 2 12 1 it tells me: PDF have 3 Car Wheel Product, and 1 Car Template Header above; I wrote SQL code, but does not work properly: CREATE TABLE `pdf_created` ( `id` INT(10) UNSIGNED NOT NULL UNIQUE AUTO_INCREMENT, `pdf_id` INT(10) NOT NULL, `item_type` INT(3) UNSIGNED NOT NULL, `item_id` INT(10) UNSIGNED NOT NULL, `quantity` INT(3) NOT NULL, PRIMARY KEY (`id`), KEY `FK_pdf_id` (`pdf_id`), CONSTRAINT `FK_pdf_id`

Primary and Foreign Key at the same time

有些话、适合烂在心里 提交于 2019-11-27 11:28:41
问题 Would it be possible in SQL Server 2008 to have a table created with 2 columns that are at the same time primary and foreign keys? If yes, how would such a code look like? I've searched and came up with nothing. 回答1: Sure, no problem: CREATE TABLE dbo.[User] ( Id int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(1024) NOT NULL ); CREATE TABLE [Group] ( Id int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(1024) NOT NULL ); CREATE TABLE [UserToGroup] ( UserId int NOT NULL, GroupId int NOT NULL,

MongoDB normalization, foreign key and joining

梦想与她 提交于 2019-11-27 11:00:28
Before I dive really deep into MongoDB for days, I thought I'd ask a pretty basic question as to whether I should dive into it at all or not. I have basically no experience with nosql. I did read a little about some of the benefits of document databases, and I think for this new application, they will be really great. It is always a hassle to do favourites, comments, etc. for many types of objects (lots of m-to-m relationships) and subclasses - it's kind of a pain to deal with. I also have a structure that will be a pain to define in SQL because it's extremely nested and translates to a

Django Rest Framework - Get related model field in serializer

雨燕双飞 提交于 2019-11-27 10:28:10
问题 I'm trying to return a HttpResponse from Django Rest Framework including data from 2 linked models. The models are: class Wine(models.Model): color = models.CharField(max_length=100, blank=True) country = models.CharField(max_length=100, blank=True) region = models.CharField(max_length=100, blank=True) appellation = models.CharField(max_length=100, blank=True) class Bottle(models.Model): wine = models.ForeignKey(Wine, null=False) user = models.ForeignKey(User, null=False, related_name=

How to remove foreign key constraint in sql server?

丶灬走出姿态 提交于 2019-11-27 10:04:56
问题 I want to remove foreign key from another table so i can insert values of my choice. I am new in databases so please tell me correct sql query to drop or remove foreign key value. 回答1: Try following ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME> Refer : http://www.w3schools.com/sql/sql_foreignkey.asp 回答2: Its wrong to do that in refer to referential integrity, because once its broken its not easy to turn it on again without having to go through the records and delete the ones

Disabling foreign key checks on the command line

给你一囗甜甜゛ 提交于 2019-11-27 09:59:54
问题 I have a backup script for my MySQL database, using mysqldump with the --tab option so it produces a .sql file for the structure and a .txt file (pipe-separated) for the content. Some tables have foreign keys, so when I import it I'm getting the error: ERROR 1217 (23000) at line 8: Cannot delete or update a parent row: a foreign key constraint fails I know about using SET FOREIGN_KEY_CHECKS=0 (and SET FOREIGN_KEY_CHECKS=1 afterward). If I add those to each .sql file then the import works. But

MySQL DROP all tables, ignoring foreign keys

穿精又带淫゛_ 提交于 2019-11-27 09:55:53
Is there a nice easy way to drop all tables from a MySQL database, ignoring any foreign key constraints that may be in there? Dion Truter I found the generated set of drop statements useful, and recommend these tweaks: Limit the generated drops to your database like this: SELECT concat('DROP TABLE IF EXISTS `', table_name, '`;') FROM information_schema.tables WHERE table_schema = 'MyDatabaseName'; Note: This does not execute the DROP statements, it just gives you a list of them. You will need to cut and paste the output into your SQL engine to execute them. Note, per http://dev.mysql.com/doc

What Kind of Relationship is Between These Tables?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 09:38:48
I have two tables that have foreign keys to each other's primary key. This DB is in French. I will translate the two tables that I want to you to understand. Atelier Cuisine ==> Kitchen Cuisinier == > Cooking chef So in this picture we see that in the Kitchen table we have a PK referenced by the FK from the Cooking chef table; in the Cooking chef table we have a PK referenced by the FK from the Kitchen table. So I am confused. I don't understand this kind of relationship between these tables. And I hope to check my query that I did to create these two tables if its correct CREATE TABLE

How to use dynamic foreignkey in Django?

我是研究僧i 提交于 2019-11-27 09:35:03
问题 I want to connect a single ForeignKey to two different models. For example: I have two models named Casts and Articles , and a third model, Faves , for favoriting either of the other models. How can I make the ForeignKey dynamic? class Articles(models.Model): title = models.CharField(max_length=100) body = models.TextField() class Casts(models.Model): title = models.CharField(max_length=100) body = models.TextField() class Faves(models.Model): post = models.ForeignKey(**---CASTS-OR-ARTICLES--

Defining foreign key relationships for Rails' models

五迷三道 提交于 2019-11-27 09:24:27
问题 I have a Comment class with a :foreign_key of post_id in the Post class. class Comment < ActiveRecord::Base belongs_to :post, :class_name => "Post", :foreign_key => "post_id", :counter_cache => true belongs_to :author, :class_name => "User", :foreign_key => "author_id" end But my CreateComments migration does not define a database-level foreign key: class CreateComments < ActiveRecord::Migration def self.up create_table :comments do |t| t.column "post_id", :integer, :default => 0, :null =>