foreign-keys

When to use a foreign key in MySQL

好久不见. 提交于 2019-12-06 21:38:22
问题 Is there official guidance or a threshold to indicate when it is best practice to use a foreign key in a MySQL database? Suppose you created a table for movies. One way to do it is to integrate the producer and director data into the same table. (movieID, movieName, directorName, producerName). However, suppose most directors and producers have worked on many movies. Would it be best to create two other tables for producers and directors, and use a foreign key in the movie table? When does it

MySQL index name and foreign key name must be different for different tables?

混江龙づ霸主 提交于 2019-12-06 20:59:37
问题 MySQL index name and foreign key name must be different for different tables? For example, Two tables both have the same field(profile_id) which are belonging to a third table(profiles). So I want to make the profile_id indexed and constrain it as a foreign key. Could the index name be named "profile_id_idx" in both tables? And "profile_id_fk" as name of foreign key for both too? 回答1: Foreign Key names must be unique across all tables in all databases. Index names may be re-used in different

EntityFramework 6 AddOrUpdate not working with compound or composite primary key

一曲冷凌霜 提交于 2019-12-06 20:06:03
问题 The issue has been my weekend nightmare... I have a table where AddOrUpdate is not working correctly, it keeps adding but never updating. All I want to do is when I add a new entity to the table using AddOrUpdate I want it to check the AppointmentId and CompletionCodeId columns and if they match than update, otherwise add. Table structure: CREATE TABLE [dbo].[AppointmentCodes] ( [Id] INT IDENTITY (1, 1) NOT NULL, [AppointmentId] INT NOT NULL, [Quantity] INT NOT NULL, [CompletionCodeId] INT

Error 1005 in MySQL (from foreign key syntax?)

本小妞迷上赌 提交于 2019-12-06 19:47:34
I'm getting an error while creating the following tables in MySQL. Here's the error: ERROR 1005 (HY000): Can't create table 'Works' (errno: 150) Could you guys take a look at the foreign keys of Works() and let me know if you see anything weird about them that would cause this error to happen? Here's my overall schema: create table Employee( Lastname varchar(10), FirstName varchar(10), MidInitial char(1), gender char(1), street varchar(10), city varchar(10), primary key(Lastname, FirstName, MidInitial)); create table company( company_name varchar(20), city varchar(10), primary key(company_name

SQLAlchemy ForeignKey can't find table

一个人想着一个人 提交于 2019-12-06 19:27:24
问题 Getting this error when I try to instantiate the ConsumerAdvice class. Foreign key associated with column 'tbConsumerAdvice.ConsumerAdviceCategory_ID' could not find table 'tbConsumerAdviceCategories' with which to generate a foreign key to target column 'ID_ConsumerAdviceCategories' class ConsumerAdviceCategory(Base): __tablename__ = 'tbConsumerAdviceCategories' __table_args__ = {'schema':'dbo'} ID_ConsumerAdviceCategories = Column(INTEGER, Sequence('idcac'),\ primary_key=True) Name = Column

Primary key and foreign key at the same time with doctrine 2

冷暖自知 提交于 2019-12-06 19:19:13
问题 I have the two tables : table A with id as primary key table B with id as primary key and foreign key Explanation on short: I need to have in table B a primary key that also to be a foreign key that points to table A 's primary key. Can anybody explain me how to map this by annotations in Doctrine 2? Note: I tried it By this : class A { /** * @var bigint $id * * @Column(name="id", type="bigint", nullable=false) * @Id * @GeneratedValue(strategy="IDENTITY") */ private $a_id; ... and B table:

EF One-to-many Foreign Keys without child navigation properties

微笑、不失礼 提交于 2019-12-06 19:12:36
问题 Using code-first Entity Framework and .NET 4, I'm trying to create a one-to-many relationship between parents to children: public class Parent { [Key] public int ParentId { get; set; } [Required] public string ParentName { get; set; } public IEnumerable<Child> Children { get; set; } } public class Child { [Key] public int ChildId { get; set; } [ForeignKey] public int ParentId { get; set; } [Required] public string ChildName { get; set; } } As pointed out here, in order for foreign key

How to find out whether a model's column is a foreign key?

早过忘川 提交于 2019-12-06 19:00:32
问题 I'm dynamically storing information in the database depending on the request: // table, id and column are provided by the request table_obj = getattr(models, table) record = table_obj.objects.get(pk=id) setattr(record, column, request.POST['value']) The problem is that request.POST['value'] sometimes contains a foreign record's primary key (i.e. an integer) whereas Django expects the column's value to be an object of type ForeignModel: Cannot assign "u'122'": "ModelA.b" must be a "ModelB"

Defining a Foreign key constraint in H2 Databases

谁说胖子不能爱 提交于 2019-12-06 18:21:54
问题 I am new in coding so I made a tables in SQL server and it worked, so i used the same command in H2 and it said I have a syntax problems with the second table, someone can help? CREATE TABLE TOURISTINFO( TOURISTINFO_ID INT PRIMARY KEY, NAME VARCHAR(25) NOT NULL, NATIONALITY VARCHAR(15) NOT NULL ) CREATE TABLE PLANETICKETS( DESTINATION VARCHAR(10) NOT NULL, TICKETPRICE NUMERIC(8,2) NOT NULL, TOURISTINFO_ID INT FOREIGN KEY REFERENCES TOURISTINFO ) The error is Syntax error in SQL statement

Multiplicity constraint violations with optional-required EF Code First + Fluent relationship?

折月煮酒 提交于 2019-12-06 16:05:59
For some reason I had my made my mind a while back on an EF 6 project that I would try to avoid naming foreign keys. I defined much of the model without testing it incrementally and so I have been running into multiplicity and incomplete Fluent API definition issues: A relationship from the 'User_InternalAuth' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'User_InternalAuth_Target' must also in the 'Deleted' state. In one case, here is the code: nModelBuilder.Entity<User>() .HasOptional<InternalAuth>(u => u.InternalAuth) .WithRequired(a => a.User)