foreign-keys

Django: UserProfile with Unique Foreign Key in Django Admin

白昼怎懂夜的黑 提交于 2019-11-28 08:36:48
I have extended Django's User Model using a custom user profile called UserExtension . It is related to User through a unique ForeignKey Relationship, which enables me to edit it in the admin in an inline form! I'm using a signal to create a new profile for every new user: def create_user_profile(sender, instance, created, **kwargs): if created: try: profile, created = UserExtension.objects.get_or_create(user=instance) except: pass post_save.connect(create_user_profile, sender=User) (as described here for example: Extending the User model with custom fields in Django ) The problem is, that, if

Cannot add foreign key constraint - MySQL ERROR 1215 (HY000)

雨燕双飞 提交于 2019-11-28 08:34:15
I am trying to create database for gym management system, but I can't figure out why I am getting this error. I've tried to search for the answer here, but I couldn't find it. ERROR 1215 (HY000): Cannot add foreign key constraint CREATE TABLE sales( saleId int(100) NOT NULL AUTO_INCREMENT, accountNo int(100) NOT NULL, payName VARCHAR(100) NOT NULL, nextPayment DATE, supplementName VARCHAR(250), qty int(11), workoutName VARCHAR(100), sDate datetime NOT NULL DEFAULT NOW(), totalAmount DECIMAL(11,2) NOT NULL, CONSTRAINT PRIMARY KEY(saleId, accountNo, payName), CONSTRAINT FOREIGN KEY(accountNo)

Why is the foreign key part of the primary key in an identifying relationship?

老子叫甜甜 提交于 2019-11-28 08:22:45
I'm trying to understand a concept rather than fixing a piece of code that won't work. I'll take a general example of a form (parent table) and a form field (child table). Logically , this would be an identifying relationship, since a form field cannot exist without a form. This would make me think that in order to translate the logical relationship into the technical relationship, a simple NOT NULL for the form_id field in the form_field table would suffice. (See the left part of above screenshot.) However, when I add an identifying relationship using MySQL Workbench, form_id is not only NOT

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

大憨熊 提交于 2019-11-28 08:18:51
问题 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

Disabling foreign key constraint, still can't truncate table? (SQL Server 2005)

本秂侑毒 提交于 2019-11-28 08:07:09
I have a table called PX_Child that has a foreign key on PX_Parent. I'd like to temporarily disable this FK constraint so that I can truncate PX_Parent. I'm not sure how this goes however. I've tried these commands ALTER TABLE PX_Child NOCHECK CONSTRAINT ALL ALTER TABLE PX_Parent NOCHECK CONSTRAINT ALL (truncate commands) ALTER TABLE PX_Child CHECK CONSTRAINT ALL ALTER TABLE PX_Parent CHECK CONSTRAINT ALL But the truncate still tells me it can't truncate PX_Parent because of a foreign key constraint. I've looked all around the net and can't seem to find what I'm doing wrong, sorry for the

MySQL RESTRICT and NO ACTION

隐身守侯 提交于 2019-11-28 07:55:15
What's the difference in a MySQL FK between RESTRICT and NO ACTION ? From the doc they seem exactly the same. Is this the case? If so, why have both? From MySQL Documentation: https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION is the same as RESTRICT . They are identical in MySQL. In the SQL 2003 standard there are 5 different referential actions: CASCADE RESTRICT NO ACTION SET NULL SET DEFAULT The difference between NO

How to I show a list of ForeignKey reverse lookups in the DJango admin interface?

不羁岁月 提交于 2019-11-28 07:31:46
I have a couple of models: class Customer(models.Model): customer_name = models.CharField(max_length=200) def __unicode__(self): return self.customer_name class Meta: ordering = ('customer_name',) class Unit(models.Model): unit_number = models.IntegerField() rentable = models.BooleanField() owner = models.ForeignKey(Customer, related_name='units', blank=True, null=True) def __unicode__(self): return str(self.unit_number) class Meta: ordering = ('unit_number',) I have the admin interface working fine when I'm adding a unit (I can select which customer to assign it to) but when I go to create

Changing MySQL primary key when foreign key contraints exist

空扰寡人 提交于 2019-11-28 07:27:08
问题 I have two already-existing tables which look (in part) roughly like this: CREATE TABLE parent ( old_pk CHAR(8) NOT NULL PRIMARY KEY ) ENGINE=InnoDB; CREATE TABLE child ( parent_key CHAR(8), FOREIGN KEY (parent_key) REFERENCES parent(old_pk) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE=InnoDB; I want to add a new auto-incrementing integer id column to parent and use it as the primary key instead, while still keeping old_pk as a unique key and allowing other tables like child to reference it

What exactly is a foreign key?

不羁的心 提交于 2019-11-28 07:01:26
Ok. So I know what a primary key in DB is. If you have a table in a database, a primary key is a single value that is unique to each row in your table. For example: id | name | whatever ------------------------- 1 Alice .... 2 Bob .... 45 Eve .... 988 .... .... So I need a good, simple example to explain what exactly a foreign key is. Because I just don't get it :) Edit: OK it's pretty easy, I guess I was over-complicating the problem. So one final question, the only restriction on foreign keys is that it they are a valid primary key value in the table I am referring to? A foreign key is a

How to have a foreign key pointing to two primary keys?

房东的猫 提交于 2019-11-28 06:31:23
问题 I'm trying to simplify a database structure, and I have two tables matches and team_statistics : Here in the team_statistics table the team_statistics.team_id should be a foreign key that references matches.teams_id and matches.teams_id1 and similarly team_statistics.group_id should be a foreign key referencing matches.groups_id and matches.groups_id1 How to do this in PostgreSQL? If there are other ways of doing this by having another table between matches and team_statistics I'm open for