foreign-keys

Django DetailView Filter by Foreign Key

风流意气都作罢 提交于 2019-12-11 01:46:51
问题 I am little puzzled and would like to utilized the DetailView functionality using the foreign key as my filter to display the data. Basically my model looks like this: class Category(models.Model): name = models.CharField(max_length=30) slug = models.SlugField(help_text="A short name for Category") def __unicode__(self): return self.name class Meta: ordering = ["-name"] verbose_name_plural = "categories" class Publisher(models.Model): name = models.CharField(max_length=30) slug = models

MySql memory engine do not check on update foreign key?

旧时模样 提交于 2019-12-11 01:22:37
问题 After making a few tests, I found that foreign keys on MEMORY engine miss one of the checks for consistency. To explain better, here a similar example in InnoDb CREATE TABLE testInnoDb1 ( c1 int(11) PRIMARY KEY ) ENGINE=InnoDB; CREATE TABLE testInnoDb2 ( c1 int(11) PRIMARY KEY, FOREIGN KEY (c1) REFERENCES testInnoDb1(c1) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE=InnoDB; INSERT INTO testInnoDb1 (c1) VALUES (42); INSERT INTO testInnoDb2 (c1) VALUES (42); -- INSERT INTO testInnoDb2 (c1)

Multiple foreign keys in one table to one other table in mysql

元气小坏坏 提交于 2019-12-11 00:22:15
问题 I got two tables in my database: user and call. User exists of 3 fields: id, name, number and call : id, 'source', 'destination', 'referred', date. I need to monitor calls in my app. The 3 ' ' fields above are actually userid numbers. Now I'm wondering, can I make those 3 field foreign key elements of the id-field in table user? 回答1: Yes - you can ;-) Just define all three foreign keys to refer to the id column in User . 回答2: Something alike should do the work: ALTER TABLE call ADD CONSTRAINT

SQL Error : integrity constraint violation : foreign key no parent

二次信任 提交于 2019-12-10 23:14:04
问题 I'm struggling to find what I'm doing wrong with my query. I did not create the database but here is the structure : 3 TABLES : vehicule : brand, motor, price, name, id option : id, description, price vehicule_option : id_vehicule, id_option I guess that vehicule_option has 2 foreign keys but I can not find the problem with my addings. Here's my code which first part works fine : public boolean create(Vehicule v){ String query = "INSERT INTO vehicule (MARQUE, MOTEUR, PRIX, NOM) VALUES (";

SQL foreign key references

瘦欲@ 提交于 2019-12-10 22:24:47
问题 Does FOREIGN KEY (a) REFERENCES A(a),FOREIGN KEY (b) REFERENCES A(b) Has the same meanning as in : FOREIGN KEY (a,b) REFERENCES A(a,b)? 回答1: No. Having two references statement means that both a and b appear in A independently . That is, they are valid values, but they don't have to appear together. Think of "February 30". It has a valid month and a valid day of month, when each are checked separately. Having a single references statement means that a and b appear in A together . Hence,

How to set the id of a foreign key id #sf2 #doctrine2

。_饼干妹妹 提交于 2019-12-10 22:17:16
问题 I'm trying to manually set an foreign key id to an object, but didn't find how to do it class Item { /** * @ORM\ManyToOne(targetEntity="MyBundle\Entity\ItemType", inversedBy="itemTypes") * @ORM\JoinColumn(name="type_id", referencedColumnName="id") */ protected $item_type; } Is there a way to do something link that? $item = new Item(); $item->setItemTypeId(1); // This generate an error. Or do i have to do like that ? $item = new Item(); $type = Repository::RetrieveById(1); $item->setItemType(

How to enable Foreign Key constraint in SQLITE with Objective C

给你一囗甜甜゛ 提交于 2019-12-10 21:19:10
问题 Today I noticed that Foreign key constraint on my SQLite table does not work. After reading on Stack Overflow, I found out that this should be enabled. So, I was looking for code snippet for doing that. So far, I could only find this: [self.db executeUpdate:@"PRAGMA foreign_keys=ON"]; But this does not seem to work for me, as compiler always complains. I saw people use this line for FMDatabase type (I don't even know what is it). So, how do I enable foreign key constraint, if I open database

Foreign Key Annotation in MVC

对着背影说爱祢 提交于 2019-12-10 20:25:36
问题 I have two tables as State(StateID int,StateName string) City(CityID int,StateID int,CityName string) I am working on MVC4 with code first approach.The code,i am using for State and City Model is - For State: [Key] public int StateID{get;set;} public string StateName{get;set;} For City: [Key] public int CityID{get;set;} [ForeignKey("State")] public string StateID{get;set;} public virtual State State{get;set;} This code is creating a foreignkey for StateID of State Table.All i wanted is

Foreign Key in MySQL : ERROR 1005

谁都会走 提交于 2019-12-10 20:21:09
问题 I'm a bit confused about adding foreign keys in MySQL What I'm trying to do is reference the Students primary key with: CREATE TABLE Enrolled(sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid, cid), FOREIGN KEY (sid) REFERENCES Students); However, what I get is ERROR 1005 (HY000): Can't create table 'test_db.Enrolled' (errno: 150) I searched around and found MySQL "ERROR 1005 (HY000): Can't create table 'foo.#sql-12c_4' (errno: 150)" However, my Students table already has a primary

how to set foreign key during form completion (python/django)

我只是一个虾纸丫 提交于 2019-12-10 20:15:25
问题 During form processing I'd like to be able to set a foreign key field on a model object without the user having to select the key from a dropdown. For instance: #models.py class AAA(models.Model): some_field = models.TextField() class BBB(models.Model): another_field = models.TextField() key_field = models.ForeignKey('AAA') The user will navigate from a view showing an instance of 'AAA' to a create_object style view that will create an instance of 'BBB' given a parameter referring to 'AAA'.