foreign-keys

Adding a column as a foreign key gives ERROR column referenced in foreign key constraint does not exist

半世苍凉 提交于 2019-12-03 05:23:53
问题 I have the following set up, CREATE TABLE auth_user ( id int PRIMARY KEY ); CREATE TABLE links_chatpicmessage (); I'm trying to add a column named sender to links_chatpicmessage which is a foreign key to another table called auth_user 's id column. To achieve the above, I'm trying the following on terminal: ALTER TABLE links_chatpicmessage ADD FOREIGN KEY (sender) REFERENCES auth_user; But this gives me an error: ERROR: column "sender" referenced in foreign key constraint does not exist How

Schema specified is not valid. Errors: The relationship was not loaded because the type is not available

╄→尐↘猪︶ㄣ 提交于 2019-12-03 05:23:10
I wish to reference the OrderAddress model twice in my Order model; once as a ShippingAddress and once as a BillingAdress . On the other side, I want my OrderAddress model to have a list of OrderAddresses . OrderAddress Model public enum AddressType { Billing, Shipping, Contact } public class OrderAddress : BaseModel { public AddressType AddressType { get; set; } public bool IsPrimary { get; set; } public string Address { get; set; } public string CityStateZip { get; set; } public string ContactName { get; set; } public string PhoneNumber { get; set; } public string FaxNumber { get; set; }

SQL Server 2005: Nullable Foreign Key Constraint

若如初见. 提交于 2019-12-03 05:17:14
I have a foreign key constraint between tables Sessions and Users. Specifically, Sessions.UID = Users.ID. Sometimes, I want Sessions.UID to be null. Can this be allowed? Any time I try to do this, I get an FK Constraint Violation. Specifically, I'm inserting a row into Sessions via LINQ. I set the Session.User = null; and I get this error: An attempt was made to remove a relationship between a User and a Session. However, one of the relationship's foreign keys (Session.UID) cannot be set to null. However, when I remove the line that nulls the User property, I get this error on my SubmitChanges

Django Foreign Key: get related model?

一笑奈何 提交于 2019-12-03 05:16:14
Is it possible to get the related model of a foreign key through the foreign key field itself? For example, if I have 3 models: class ModelA(models.Model) field1 = models.CharField(max_length=10) class ModelB(models.Model) field1 = models.CharField(max_length=10) class ModelC(models.Model) field1 = models.CharField(max_length=10) field2 = models.ForeignKey(ModelA) field3 = models.ForeignKey(ModelB) and I want to do: for field in ModelC._meta.fields: if field.get_internal_type() == "ForeignKey": #get the related model for field e.g. ModelA or ModelB Is this possible using just the models

MySQL: Creating table with FK error (errno 150)

早过忘川 提交于 2019-12-03 05:00:18
I've created a model with MySQL Workbench and am now attempting to install it to a mysql server. Using File > Export > Forward Engineer SQL CREATE Script... it outputs a nice big file for me, with all the settings I ask for. I switch over to MySQL GUI Tools (the Query Browser specifically) and load up this script (note that I'm going form one official MySQL tool to another). However, when I try to actually execute this file, I get the same error over and over SQLSTATE[HY000]: General error: 1005 Can't create table './srs_dev/location.frm' (errno: 150) "OK", I say to myself, something is wrong

MongoDB storing arrays of ObjectId's

我的未来我决定 提交于 2019-12-03 04:58:50
In my database I have to store an array of object ids. What should I use? Something like this: [ObjectId("50350e12a36feb1be6000364"), ObjectId("57350e12a37fef1be6000922"), ObjectId("10350e17d34ffb1be6200925")] or something like this: ["50350e12a36feb1be6000364", "57350e12a37fef1be6000922", "10350e17d34ffb1be6200925"] I could save space with the second, and then cast to ObjectId , but am I loosing anything by using this approach? Do ObjectId s behave like foreign keys in relational databases? I would definitely go with the first approach, storing the ObjectId s directly. This saves space, as

Understanding Relationships & Foreign Keys in Mongoose

佐手、 提交于 2019-12-03 04:52:07
In MongoDB/Mongoose, how do I define a relationship? I think there are a few ways I've seen, but I'm not sure I understand the differences or when do I use which. I am using Mongoose 3 I've defined Todo and TodoList model, where the relationship is obvious. So following the docs http://mongoosejs.com/docs/documents.html , I've defined classes like: # Todo.coffee mongoose = require "mongoose" todoSchema = mongoose.Schema name: String desc: String dueOn: Date completedOn: Date module.exports = mongoose.model "Todo", todoSchema # TodoList.coffee mongoose = require "mongoose" Todo = require ".

Django migration error :you cannot alter to or from M2M fields, or add or remove through= on M2M fields

醉酒当歌 提交于 2019-12-03 04:30:39
问题 I'm trying to modify a M2M field to a ForeignKey field. The command validate shows me no issues and when I run syncdb : ValueError: Cannot alter field xxx into yyy they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields) So I can't make the migration. class InstituteStaff(Person): user = models.OneToOneField(User, blank=True, null=True) investigation_area = models.ManyToManyField(InvestigationArea, blank=True,) investigation_group =

MySQL: How do I find out which tables reference a specific table?

谁都会走 提交于 2019-12-03 04:14:24
问题 I want to drop a table but it is referenced by one or more other tables. How can I find out which tables are referencing this table without having to look at each of the tables in the database one by one? 回答1: select table_name from information_schema.KEY_COLUMN_USAGE where table_schema = 'my_database' and referenced_table_name = 'my_table_here'; This works. 回答2: select table_name from information_schema.referential_constraints where referenced_table_name = 'parent table here'; 回答3: If you

Postgresql constraint

北战南征 提交于 2019-12-03 04:11:43
I cannot seem to get this right, I am trying to modify a field to be a foreign key, with cascading delete... what am i doing wrong? ALTER TABLE my_table ADD CONSTRAINT $4 FOREIGN KEY my_field REFERENCES my_foreign_table ON DELETE CASCADE; Magnus Hagander It would help if you posted the error message. But I think you are just missing the parenthesis: ALTER TABLE my_table ADD CONSTRAINT my_fk FOREIGN KEY (my_field) REFERENCES my_foreign_table ON DELETE CASCADE; Just guessing: shouldn't you add a foreign key instead of a constraint? ALTER TABLE my_table ADD FOREIGN KEY (my_field) REFERENCES my