foreign-key-relationship

SQL Server 2008: The columns in table do not match an existing primary key or unique constraint

谁说胖子不能爱 提交于 2019-11-30 05:34:28
I need to make some changes to a SQL Server 2008 database. This requires the creation of a new table, and inserting a foreign key in the new table that references the Primary key of an already existing table. So I want to set up a relationship between my new tblTwo, which references the primary key of tblOne. However when I tried to do this (through SQL Server Management Studio) I got the following error: The columns in table 'tblOne' do not match an existing primary key or UNIQUE constraint I'm not really sure what this means, and I was wondering if there was any way around it? It means that

Laravel foreign key onDelete('cascade') not working

十年热恋 提交于 2019-11-30 04:50:41
问题 I have a many-to-many relationship between User & Role, with a role_user table. My migrations are setup as so (simplified): users table: public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->string('email')->unique(); }); } roles table: public function up() { Schema::create('roles', function(Blueprint $table) { $table->increments('id'); $table->string('name'); }); } role_user table: public function up() { Schema::create('role_user',

Slick- use foreignKey constraint and access referenced object directly as column

浪子不回头ぞ 提交于 2019-11-30 03:55:04
问题 I have two models (say, Supplier and Coffee ) and Coffee model has foreign key reference to Supplier model. During ddl, I want this relationship to exist in table creation. But I also want to be able to refer the Supplier object through Coffee object like coffeeObj.supplier.name . Below is my dummy code. I am using MappedTable, foreignKey and TypeMapper. import scala.slick.driver.H2Driver.simple._ import Database.threadLocalSession object SlickTest { // Supplier case class Supplier(id: Option

Rails multiple associations between two models

◇◆丶佛笑我妖孽 提交于 2019-11-30 03:48:41
问题 I have Flight, Person, and Glider models in a Rails 3 app. I've defined custom relationships because I need more than one foreign key referencing a Person from the flights table. Associations work but ONE-WAY only. class Flight < ActiveRecord::Base belongs_to :pilot, :class_name => "Person" belongs_to :instructor, :class_name => "Person" belongs_to :towplane_pilot, :class_name => "Person" belongs_to :airplane_instructor, :class_name => "Person" belongs_to :glider belongs_to :rep_glider,

Rails: Non id foreign key lookup ActiveRecord

旧街凉风 提交于 2019-11-30 03:02:51
I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes , this rightly triggers SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2) (2 being the that coach's id here which is my problem.) I want it to trigger SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 'David') ('David' being the that coach's user_name ) user_name is

SQLite Foreign Key

一世执手 提交于 2019-11-29 22:10:22
I'm following the instructions from the SQLite documentation at http://www.sqlite.org/foreignkeys.html however my attempt to add a foreign key is failing. Here are my create statements: CREATE TABLE checklist ( _id INTEGER PRIMARY KEY AUTOINCREMENT, checklist_title TEXT, description TEXT, created_on INTEGER, modified_on INTEGER ); CREATE TABLE item ( _id INTEGER PRIMARY KEY AUTOINCREMENT, FOREIGN KEY(checklist_id) REFERENCES checklist(_id), item_text TEXT, item_hint TEXT, item_order INTEGER, created_on INTEGER, modified_on INTEGER ); The first table is made fine. The error occurs in the second

SQL Add foreign key to existing column

十年热恋 提交于 2019-11-29 20:00:52
If I am using the following SQL command in SQL Server 2008 to update a table with a foreign key constraint: ALTER TABLE Employees ADD FOREIGN KEY (UserID) REFERENCES ActiveDirectories(id) UserID being my FK column in the Employees table. I'm trying to reference the UserID in my ActiveDirectories table. I receive this error: Foreign key 'UserID' references invalid column 'UserID' in referencing table 'Employees'. Error indicates that there is no UserID column in your Employees table. Try adding the column first and then re-run the statement. ALTER TABLE Employees ADD CONSTRAINT FK

Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?

…衆ロ難τιáo~ 提交于 2019-11-29 19:22:39
I'm having a little difficulty getting my head around relationships in Django models. Could someone explain what the difference is between a OneToOne, ManyToMany and ForeignKey? Well, there's essentially two questions here: What is the difference (in general) between one to one, many to many, and foreign key relations What are their differences specific to Django. Both of these questions are quite easily answered through a simple Google search, but as I cannot find an exact dupe of this question on SO, I'll go ahead and answer. Note that in Django, relationships should only be defined on one

Tools for discovering de facto foreign keys in databases? [closed]

强颜欢笑 提交于 2019-11-29 09:26:57
问题 A good way to quickly survey the information in a database is to apply a tool that automatically creates a database diagram of all tables and all relationships between them. In my experience, such tools use foreign keys as the relationships, which most of the databases I try them do not contain. Sure, they satisfy constraints corresponding to foreign keys, but do not enforce them. And I'll end up with a 'diagram' consisting of a bunch of unrelated tables. So what I'm looking for is software

foreign key constraint ON DELETE CASCADE not working in sqlite database on android

故事扮演 提交于 2019-11-29 06:00:00
I have "days" table created as follows "create table days(" + "day_id integer primary key autoincrement, " + "conference_id integer , " + "day_date text, " + "day_start_time text, " + "day_end_time text, " + "day_summary text, " + "day_description text)"; and i have tracks table created as follows CREATE_TABLE_TRACK = "create table track(" + "track_id integer primary key autoincrement," + "day_id integer,"+ "track_name text," + "track_description text," + " FOREIGN KEY(day_id) REFERENCES days(day_id) ON DELETE CASCADE )"; as shown above i have foreign key day_id referencing to the day_id of