foreign-keys

How to remove foreign key constraint in sql server?

孤街醉人 提交于 2019-11-28 17:09:13
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. Try following ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME> Refer : http://www.w3schools.com/sql/sql_foreignkey.asp 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 which breaks the constraints. Anyway the Syntax is as follows: ALTER TABLE Tablename DROP CONSTRAINT ContName; See

Disabling foreign key checks on the command line

痴心易碎 提交于 2019-11-28 16:58:37
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 then obviously on the next mysqldump those get overwritten. I also tried running it as a separate

Foreign keys in django admin list display

半腔热情 提交于 2019-11-28 16:42:15
问题 If a django model contains a foreign key field, and if that field is shown in list mode, then it shows up as text , instead of displaying a link to the foreign object. Is it possible to automatically display all foreign keys as links instead of flat text? (of course it is possible to do that on a field by field basis, but is there a general method?) Example : class Author(models.Model): ... class Post(models.Model): author = models.ForeignKey(Author) Now I choose a ModelAdmin such that the

Django “Cannot add or update a child row: a foreign key constraint fails”

こ雲淡風輕ζ 提交于 2019-11-28 16:18:53
问题 I have a model Coupon , and a model Photo with a ForeignKey to it: class Photo(models.Model): coupon = models.ForeignKey(Coupon, related_name='description_photos') title = models.CharField(max_length=100) image = models.ImageField(upload_to='images') I set up inlines in the admin so now I'm able to add photos to a coupon from the admin. I attempt to add one, and upload is successful, but then I get Django's debug page with this error: IntegrityError at /admin/coupon/coupon/321/ (1452, 'Cannot

MySQL terminology “constraints” vs “foreign keys” difference?

谁说胖子不能爱 提交于 2019-11-28 16:11:58
I'm looking at the MySQL docs here and trying to sort out the distinction between FOREIGN KEYs and CONSTRAINTs. I thought an FK was a constraint, but the docs seem to talk about them like they're separate things. The syntax for creating an FK is (in part)... [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) REFERENCES tbl_name (index_col_name,...) So the "CONSTRAINT" clause is optional. Why would you include it or not include it? If you leave it out does MySQL create a foreign key but not a constraint? Or is it more like a "CONSTRAINT" is nothing more than a name for you FK,

Defining foreign key relationships for Rails' models

不打扰是莪最后的温柔 提交于 2019-11-28 15:52:47
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 => false t.column "author", :string, :default => "", :limit => 25, :null => false t.column "author_email",

How to use dynamic foreignkey in Django?

白昼怎懂夜的黑 提交于 2019-11-28 15:48:30
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---**) user = models.ForeignKey(User,unique=True) Is this possible? vikingosegundo Here is how I do it:

How to design a database schema to support tagging with categories?

懵懂的女人 提交于 2019-11-28 15:26:03
I am trying to so something like Database Design for Tagging , except each of my tags are grouped into categories. For example, let's say I have a database about vehicles. Let's say we actually don't know very much about vehicles, so we can't specify the columns all vehicles will have. Therefore we shall "tag" vehicles with information. 1. manufacture: Mercedes model: SLK32 AMG convertible: hardtop 2. manufacture: Ford model: GT90 production phase: prototype 3. manufacture: Mazda model: MX-5 convertible: softtop Now as you can see all cars are tagged with their manufacture and model, but the

Foreign Key naming scheme

我们两清 提交于 2019-11-28 15:20:09
I'm just getting started working with foreign keys for the first time and I'm wondering if there's a standard naming scheme to use for them? Given these tables: task (id, userid, title) note (id, taskid, userid, note); user (id, name) Where Tasks have Notes, Tasks are owned by Users, and Users author Notes. How would the three foreign keys be named in this situation? Or alternatively, does it even matter at all ? Update : This question is about foreign key names, not field names! The standard convention in SQL Server is: FK_ForeignKeyTable_PrimaryKeyTable So, for example, the key between notes

postgresql foreign key syntax

蓝咒 提交于 2019-11-28 15:13:04
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 correct: CREATE TABLE students ( student_id SERIAL PRIMARY KEY, player_name TEXT); CREATE TABLE tests (