foreign-keys

Tree Structure (Foreign Keys to itself) and templates

随声附和 提交于 2019-12-11 15:27:35
问题 I have a tree structure for Categories. Categories with a Foreign key that is referring to itself. class Category(MetaData): parent = models.ForeignKey('self', blank=True, null=True, verbose_name='parent category', on_delete=models.CASCADE) name = models.CharField(max_length=255) description = models.TextField() Because I don't know the depth of the categories tree(can't use for) I need to use a recursive function: def cat_for_parents(self, cat_obj): ... if cat_obj.parent_id: p = cat_obj

sqlalchemy foreign keys works only with primary key?

穿精又带淫゛_ 提交于 2019-12-11 14:47:33
问题 Tables and foreign keys are creating ok, but working not ok: SQLite version 3.7.9 2011-11-01 00:52:41 sqlite> pragma foreign_keys=on; sqlite> sqlite> CREATE TABLE t1(id int primary key, uuid varchar(36)); sqlite> CREATE TABLE t2(id int primary key, t1_uuid varchar(36), FOREIGN KEY (t1_uuid) REFERENCES t1(uuid)); sqlite> sqlite> INSERT INTO t1(uuid) values ("uuid-1"); sqlite> INSERT INTO t2(t1_uuid) values ("uuid-1"); Error: foreign key mismatch But if i make t1(uuid) primary key, all works as

Oracle: Make a composite Key containing three Foregin keys

余生长醉 提交于 2019-12-11 13:54:44
问题 This is my code: create table orderline ( Order_No number(4) constraint orderno_fk references order_detail(Order_No), Product_Code varchar2(6) constraint productcode2_fk references product(Product_Code), Product_Size char(1) constraint productsize_fk references product_stock(Product_Size), Product_Quantity number(4) not null constraint orderline_comp primary key (Order_No,Product_Code, Product_Size) ); I get the error (with the star underneath the left parenthesis before 'Order'): ERROR at

How to make an Entity Framework property NOT NULL, but not required in form submission

吃可爱长大的小学妹 提交于 2019-12-11 13:48:31
问题 I'm using Entity Framework 4.1 with a code-first model. A common pattern is that many objects reference the user who owns them, eg. public class Item { public User Owner { get; set; } } This creates a nullable column in the DB, but since every Item must have an owner I want the column marked NOT NULL. If I use the [Required] attribute then submitting the form to create an Item results in an error. That field is never set through a form, only manually in code. 回答1: It is generally recommended

Foreign key null - performance degradation

与世无争的帅哥 提交于 2019-12-11 13:41:03
问题 I have table folder where column parent_id references on id if that folder has parent, if not then parent_id is null. Is that ok solution or I need extra table for this connection or other solution? Can foreign key be null at all, and if can is this solution will has bigger time execution ? table folder( id int primary key, //primary key in my table parent_id int references id, //foreign key on id column in same table .... ) 回答1: Yes, a foreign key can be made to accept NULL values: CREATE

MySQL Foreign Key - Cannot Resolve Table Name Close TO

怎甘沉沦 提交于 2019-12-11 13:18:16
问题 I've run into a foreign key issue that really has me stumped. The specific error I get when running my migration is: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table products add constraint products_provider_id_foreign foreign k ey ( provider_id ) references id ( providers ) on delete cascade) The MySQL SHOW ENGINE INNODB STATUS; result under Latest Foreign Key Error is: 2016-02-22 21:38:17 11debc000 Error in foreign key constraint of table testdb/#sql

What will these foreign keys do?

拥有回忆 提交于 2019-12-11 12:55:48
问题 I used an online SQL builder to help design some MySQL tables I'm working on. Foreign keys always confuse me. The code I came up with tries to add these 4 foreign keys but I want to make sure that I want them before I add them. ALTER TABLE `users` ADD FOREIGN KEY (user_id) REFERENCES `settings` (`user_id`); ALTER TABLE `logins` ADD FOREIGN KEY (user_id) REFERENCES `users` (`user_id`); ALTER TABLE `mail_threads` ADD FOREIGN KEY (folder_id) REFERENCES `mail_folders` (`folder_id`); ALTER TABLE

Does every column contained in a composite FK need an extra Index on it?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 12:55:34
问题 I usually put index on my FKs to speeds up my queries, but what if I have a grouped index on two columns which one of them is FK. Is it necessary to put index on it too? As you may know, in this condition dm_db_missing_index_details keeps suggesting one missing index. I would appreciate if anybody could tell if it's really necessary or not? 回答1: Only if the composite index has the FK as the leftmost key. An index on (FK, Foo) will cover the (FK) missing index. But an index on (Foo, FK) will

Implement/use foreign keys in SQLite?

て烟熏妆下的殇ゞ 提交于 2019-12-11 12:54:54
问题 How can I implement a foreign key in SQLite? I was thinking something like this: CREATE TABLE job (_id INTEGER PRIMARY KEY AUTOINCREMENT, employer_id INTEGER, ...); CREATE TABLE employer(_id INTEGER, employer_name TEXT NOT NULL, ...); Where employer_id is the _id from the table employer . Would this work? Is there another fast, maybe less prone to error way? Maybe with triggers? 回答1: Maybe I don't understand the question, but if it's the constraint you want, just do this: ALTER TABLE Job ADD

Would “dereferencing” a foreign key in SQL theoretically work?

馋奶兔 提交于 2019-12-11 11:51:31
问题 Suppose we have 2 tables, a and b: CREATE TABLE a (id_a INT NOT NULL PRIMARY KEY, id_b INT NOT NULL) INDEX fk_id_b (id_b ASC), CONSTRAINT fk_id_b FOREIGN KEY (id_b) REFERENCES b (id_b); CREATE TABLE b (id_b INT NOT NULL PRIMARY KEY, b_data INT NOT NULL); So a has the following columns: id_a and id_b , where id_b is a foreign key to b s id_b . When I want to get the associated b_data from a, I have to do a join: SELECT id_a, b_data FROM a JOIN b ON a.id_b=b.id_b; It works fine, but it's long,