foreign-keys

Foreign Key Not Populating with Primary Key Values

核能气质少年 提交于 2019-11-27 05:41:12
I have searched for an answer but am not finding it. I have 2 tables. Both have an auto-generated PK. The PK from table 2 is an FK in table 1. Since they are both auto-generated I assumed the FK in table 1 would populate with the value that is auto-generated from table 2 but it is not working. The FK in table 1 ends up null. Here is my SQL code for creating table 1: CREATE TABLE Employee_tbl ( EmployeeID int PRIMARY KEY IDENTITY, LastName varchar(20) not null, FirstName varchar(20) not null, Age varchar(3) not null, JobID int FOREIGN KEY REFERENCES JobTitle_tbl(JobID), ) and here is table 2:

Setting default value for Foreign Key attribute

╄→尐↘猪︶ㄣ 提交于 2019-11-27 05:38:34
问题 What is the best way to set a default value for a foreign key field in a model? Suppose I have two models, Student and Exam with student having exam_taken as foreign key. How would I ideally set a default value for it? Here's a log of my effort class Student(models.Model): .... ..... exam_taken = models.ForeignKey("Exam", default=1) Works, but have a hunch there's a better way. def get_exam(): return Exam.objects.get(id=1) class Student(models.Model): .... ..... exam_taken = models.ForeignKey

MySQL Foreign Key On Delete

强颜欢笑 提交于 2019-11-27 05:33:39
问题 I am trying to figure out relationships and deletion options. I have two tables, User and UserStaff , with a 1:n relationship from User to UserStaff (a user can have multiple staff members). When my User is deleted, I want to delete all of the UserStaff tables associated with that User . When my UserStaff is deleted, I don't want anything to happen to User . I understand that this is a cascading relationship, but I'm not sure which way. i.e. Do I select the existing foreign key in my

CONSTRAINT to check values from a remotely related table (via join etc.)

若如初见. 提交于 2019-11-27 05:01:30
I would like to add a constraint that will check values from related table. I have 3 tables: CREATE TABLE somethink_usr_rel ( user_id BIGINT NOT NULL, stomethink_id BIGINT NOT NULL ); CREATE TABLE usr ( id BIGINT NOT NULL, role_id BIGINT NOT NULL ); CREATE TABLE role ( id BIGINT NOT NULL, type BIGINT NOT NULL ); (If you want me to put constraint with FK let me know.) I want to add a constraint to somethink_usr_rel that checks type in role ("two tables away"), e.g.: ALTER TABLE somethink_usr_rel ADD CONSTRAINT CH_sm_usr_type_check CHECK (usr.role.type = 'SOME_ENUM'); I tried to do this with

Modelling polymorphic associations database-first vs code-first

时间秒杀一切 提交于 2019-11-27 04:37:44
We have a database in which one table contains records that can be child to several other tables. It has a "soft" foreign key consisting of the owner's Id and a table name. This (anti) pattern is know as "polymorphic associations". We know it's not the best database design ever and we will change it in due time, but not in the near future. Let me show a simplified example: Both Event , Person , and Product have records in Comment. As you see, there are no hard FK constraints. In Entity Framework it is possible to support this model by sublassing Comment into EventComment etc. and let Event

Renaming foreign-key columns in MySQL

谁都会走 提交于 2019-11-27 04:35:06
We're trying to rename a column in MySQL (5.1.31, InnoDB) that is a foreign key to another table. At first, we tried to use Django-South, but came up against a known issue: http://south.aeracode.org/ticket/243 OperationalError: (1025, "Error on rename of './xxx/#sql-bf_4d' to './xxx/cave_event' (errno: 150)") AND Error on rename of './xxx/#sql-bf_4b' to './xxx/cave_event' (errno: 150) This error 150 definitely pertains to foreign key constraints. See e.g. What does mysql error 1025 (HY000): Error on rename of './foo' (errorno: 150) mean? http://www.xaprb.com/blog/2006/08/22/mysqls-error-1025

Can a foreign key act as a primary key?

浪子不回头ぞ 提交于 2019-11-27 04:28:22
问题 I'm currently designing a database structure for our team's project. I have this very question in mind currently: Is it possible to have a foreign key act as a primary key on another table? Here are some of the tables of our system's database design: user_accounts students guidance_counselors What I wanted to happen is that the user_accounts table should contain the IDs (supposedly the login credential to the system) and passwords of both the student users and guidance counselor users. In

SQlite - Android - Foreign key syntax

点点圈 提交于 2019-11-27 04:14:41
问题 I've been trying to get foreign keys working within my Android SQLite database. I have tried the following syntax but it gives me a force close: private static final String TASK_TABLE_CREATE = "create table " + TASK_TABLE + " (" + TASK_ID + " integer primary key autoincrement, " + TASK_TITLE + " text not null, " + TASK_NOTES + " text not null, " + TASK_DATE_TIME + " text not null, FOREIGN KEY ("+TASK_CAT+") REFERENCES "+CAT_TABLE+" ("+CAT_ID+"));"; Any ideas what I might be doing wrong? if

Hibernate Issue : Foreign key must have same number of columns as referenced primary key

与世无争的帅哥 提交于 2019-11-27 04:04:59
问题 Goal : I want to have importJobId in ImportJob as foreign key for id of allocation table, such that when we have importJobId then and then only we can have id in allocation as without Job there cannot be any allocations. ImportJob table has composite primary key as [ORGID,IMPORTJOBTYPE] and am trying to get create foreign key relationship in hibernate using <id name="id" column="ID"> <generator class="native"/> </id> <many-to-one name="importjobid" class="com.delta.pdo.admin.ImportJob"

Entity Framework EntityKey / Foreign Key problem

筅森魡賤 提交于 2019-11-27 03:30:36
问题 As the result of a form post, I'm trying to save a new Brand record. In my view, Gender is a dropdown, returning an Integer, which is populated from ViewData("gender") I've setup my link as follows: gID = CInt(Request.Form("Gender")) Brand.GenderReference.EntityKey = New EntityKey("DB_ENTITIES.Gender", "Id", gID) TryUpdateModel(Brand) DB.SaveChanges() Which results in the following error. Entities in 'DB_ENTITIES.Brand' participate in the 'FK_Brand_Gender' relationship. 0 related 'Gender'