foreign-keys

How to create foreign key that is also a primary key in MySQL?

我们两清 提交于 2019-11-30 03:01:30
This should be a fairly straightforward question, but I'm unable to find an easy answer. How do you create a foreign key that is also a primary key in MySQL? Here's my current attempt: CREATE TABLE Sale( sale_id CHAR(40), PRIMARY KEY(sale_id), discount DOUBLE, type VARCHAR(255), price DOUBLE, ); CREATE TABLE Normal_Sale( sale_id CHAR(40), PRIMARY KEY(sale_id); ); CREATE TABLE Special_Sale( sale_id CHAR(40), PRIMARY KEY(sale_id); ); What am I missing here? Thanks in advance. Add FOREIGN KEY (sale_id) REFERENCES Sale(sale_id) to each foreign table: CREATE TABLE Sale( sale_id CHAR(40), PRIMARY

How to prevent self (recursive) selection for FK / MTM fields in the Django Admin

浪子不回头ぞ 提交于 2019-11-30 02:51:05
问题 Given a model with ForeignKeyField (FKF) or ManyToManyField (MTMF) fields with a foreignkey to 'self' how can I prevent self (recursive) selection within the Django Admin (admin). In short, it should be possible to prevent self (recursive) selection of a model instance in the admin. This applies when editing existing instances of a model, not creating new instances. For example, take the following model for an article in a news app; class Article(models.Model): title = models.CharField(max

What is difference between foreign key and reference key?

对着背影说爱祢 提交于 2019-11-30 02:48:47
I am very confused about those two terms. Are they the same or different? Some books and people say they are the same and others say they are different. I tried but couldn't find a conclusive answer. I am supposing that you are talking about using the REFERENCES where the FOREIGN KEY keyword is not used, eg. author_id INTEGER REFERENCES author(id) ... instead of ... author_id INTEGER, FOREIGN KEY(author_id) REFERENCES author(id) The answer is, that it is simply shorthand syntax for the same thing. The main concern when altering between the two should be readability. "Reference key" isn't a

Problems creating a Foreign-Key relationship on Entity Framework

岁酱吖の 提交于 2019-11-30 02:43:20
i'm having trouble configuring a foreign key relationship in my Entity Framework fluent Api: Here is the head of the report: public class Testata { public Testata() { Details = new List<Dettaglio>(); } public virtual int IDTEST { get; set; } public virtual string Value { get; set; } public virtual int IDDETAIL { get; set; } public virtual string IDTESTALT { get; set; } public virtual byte[] BLOB { get; set; } public virtual IList<Dettaglio> Details { get; set; } } This is the report's detail public class Dettaglio { public virtual int IDDETAIL { get; set; } public virtual int IDTEST { get; set

adding a foreign key with Code First migration

醉酒当歌 提交于 2019-11-30 01:53:10
I have an error when trying to update my database after adding a migration. Here are my classes before add-migration public class Product { public Product() { } public int ProductId { get; set; } public string Name { get; set; } public decimal Price { get; set; } public bool Istaxable { get; set; } public string DefaultImage { get; set; } public IList<Feature> Features { get; set; } public IList<Descriptor> Descriptors { get; set; } public IList<Image> Images { get; set; } public IList<Category> Categories { get; set; } } public class Feature { public int Id { get; set; } public string Title {

SET CONSTRAINTS ALL DEFERRED not working as expected in PostgreSQL 9.3

*爱你&永不变心* 提交于 2019-11-30 01:44:45
问题 If I define tables a and b as follows: CREATE TABLE a(i integer); ALTER TABLE a ADD CONSTRAINT pkey_a PRIMARY KEY (i); CREATE TABLE b(j integer); ALTER TABLE b add CONSTRAINT fkey_ij FOREIGN KEY (j) REFERENCES a (i) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE; INSERT INTO a(i) VALUES(1); And then do the following: START TRANSACTION; SET CONSTRAINTS ALL DEFERRED; INSERT INTO b(j) VALUES(2); INSERT INTO a(i) VALUES(2); COMMIT; It produces the error below. Why is SET CONSTRAINTS not having

Drop down menu with value from another model

萝らか妹 提交于 2019-11-30 00:50:19
I have products belonging to collections. A collection is just a name. Products have a collection_id. In my _form view that is used for creation and edition of products, i'd like to have a drop down menu with the name of all collection. Problem, it seems there is no select method affiliated to form.for and i am trying to use : select(method, choices, options = {}, html_options = {}) from the doc but i do not understand it. I must write a methode to create a form? What are the choices, and the 2 options? Two parameters should be enough to populate an < option> tag. How can I have a drop down

How to update foreign key value in mysql database

南笙酒味 提交于 2019-11-29 23:19:09
I have three tables: categories, languages and categories_languages. Categories_languages is many to many table which links together categories and languages. I would like to update a foregin key value in table languages but it throws me error #1451 - Cannot delete or update a parent row: a foreign key constraint fails! CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(20) NOT NULL, `modified` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `languages` ( `id` char(2) NOT NULL,

Get Foreign Key Value

拟墨画扇 提交于 2019-11-29 22:59:21
问题 How can I get the foreign key values? I have a common vehicle model that links to the year, series, engine type, body style, transmission and drive train...all as foreign keys. I'd like to get the values of these fields for my app, but I'm stuck as to how I'd go about them. Any ideas will be highly appreciated. class Model(models.Model): model = models.CharField(max_length=15, blank=False) manufacturer = models.ForeignKey(Manufacturer) date_added = models.DateField() def __unicode__(self):

SQL Server 2000 - Query a Table’s Foreign Key relationships

坚强是说给别人听的谎言 提交于 2019-11-29 22:31:08
问题 Nearly identical to Query a Table's Foreign Key relationships, but for SQL Server 2000 For a given table 'foo', I need a query to generate a set of tables that have foreign keys that point to foo. 回答1: SELECT o2.name FROM sysobjects o INNER JOIN sysforeignkeys fk on o.id = fk.rkeyid INNER JOIN sysobjects o2 on fk.fkeyid = o2.id WHERE o.name = 'foo' 回答2: Try this T-SQL: select col_name(fkeyid, fkey) as column_name, object_name(rkeyid) as referenced_table_name, col_name(rkeyid, rkey) as