Questions tagged [foreign-keys]

Foreign keys are a data integrity feature of relational (and SQL) databases. A foreign key is a set of attributes in a relational table that is subject to a referential integrity constraint. The referential integrity constraint mandates that the values of the foreign key attributes in one table, A, must match with corresponding values in some specified table B (which may sometimes be the same table as A).

Filter by
Sorted by
Tagged with
0 votes
0 answers
7 views

_mysql_connector.MySQLInterfaceError: Duplicate key name 'locationid_index'

Referring the two foreign keys with same primary key in different table causes error in accessing # Create the 'rides' table if it doesn't exist cursor.execute(''' CREATE TABLE IF NOT ...
Santhosh Uppuluri's user avatar
-1 votes
0 answers
45 views

INSERT statement conflicted with the FOREIGN KEY constraint Id of AspNetUsers(IdentityUser) in ASP.NET Core MVC

I created A registration panel for Doctor, patient and Admin, when I'm trying to register or insert a data its gives error like this, SqlException: The INSERT statement conflicted with the FOREIGN ...
Timir Bhingradiya's user avatar
0 votes
0 answers
18 views

Limiting choices in django dynamically (call-back function?)

I am relatively new to Django and this is my first question on Stackoverflow. So pleas bear with me if not ideally formulated. My Problem: I have a Model Animal with two fields that are ForeignKey ...
Stephan's user avatar
1 vote
1 answer
36 views

GORM: Define multiple columns with same foreign key

I am creating a Golang MySQL project where I am using GORM. I have a table called accounts with fields ID uint `json:"id" gorm:"primary_key;auto_increment;not_null"`...
Anmol Garg's user avatar
0 votes
0 answers
28 views

What is wrong with my one-to-one EF Core relationship setup?

I am attempting to convert an Optimizely CMS ASP.NET application from .NET 4.x to .NET 7. I have two related EF Core entities: public class ScholarshipApplication { public virtual AppUser User { ...
Brian Chambers's user avatar
-1 votes
0 answers
20 views

saving linking between parent tables that have many-to-many relationships to be saved in a child table

I am saving linking between parent tables that have many-to-many relationships to be saved in a child table. So I have created the look up tables. I want to insert data in the child table with Table A ...
user2734994's user avatar
0 votes
1 answer
38 views

How do I map foreign keys in Entity Framework Core to an existing database?

Just as a heads up I'm completely new to this and this part has had me stumped for a bit. I haven't found a straight answer that worked (and everything else went right over my head) so please be ...
ReallyTrying's user avatar
-1 votes
1 answer
39 views

Without potentially disabling foreign keys, how do I have foreign key values repeated without having to disable/remove the foreign key constraint?

-- Values for the schedule of all matches that took place INSERT IGNORE INTO WomenWCSchedule (Bracket, Match_ID, Referee_ID, Team_1, Team_2, Date, Time, Stadium) VALUES ('Group Stage A', 'M01', '...
Necro007's user avatar
1 vote
2 answers
38 views

Postgres - how to add foreign key nullable constraint for two different columns coming from diff tables

I have three tables 1. Account 2. dept and 3. managers In the managers table I need to add both account key and dept key as foreign key but one will null and other will have value, means if inserted ...
user1551426's user avatar
0 votes
1 answer
27 views

How do I make a foreign key on a field from another database

I have a backend with a microservice architecture. Each microservice has its own database. There is a relational database (like postgres) for users with a users table. There is a second same database ...
any_test_user's user avatar
2 votes
0 answers
29 views

Managing orphaned MySQL rows when performing Laravel migration

I am attempting to add a migration that properly sets FK's on all tables that needs them. The previous developer did not use FKs. Without a doubt there will be several orphaned records where the ...
Jon Erickson's user avatar
  • 1,906
0 votes
1 answer
34 views

Laravel foreign key not working as intended

I am having trouble with Laravel's foreign key. Each user has his own posts, so when the user is logged in he can manage his posts (edit or delete them). When the logged in user creates a post it is ...
POTATO's user avatar
  • 3
-7 votes
0 answers
62 views

Cause condition in sql inner join [closed]

What is the reason that she has considered the condition in this query on the city? SELECT salesman.name AS "Salesman", customer.cust_name, customer.city FROM salesman,customer WHERE ...
Nasim's user avatar
  • 1
-3 votes
1 answer
59 views

How do I make two foreign keys a primary key?

My table MealIng has two foreign keys from two other tables that I want to make primary key. How do I specify this? This is my code. What do I need to change to get this to work? CREATE TABLE Meals ( ...
Leah's user avatar
  • 1
-1 votes
0 answers
29 views

Archiving tables with foreign key constraints in PostgreSQL

I'm archiving data from a set of tables with foreign key constraints. A job runs every night to archive data older than 6 months to a different Archive DB server and purge the data from the Live DB ...
Vamsi Krishna's user avatar
0 votes
2 answers
39 views

Altering Oracle Primary Keys [closed]

Oracle table may have one or more columns designated as the primary key. This designation occurs at the time that the table is created. Assume the table has multiple columns as part of it's primary ...
Eshe's user avatar
  • 11
-1 votes
0 answers
11 views

all tables are references of each other [duplicate]

create table Employe( nuEmp integer primary key, nomempl varchar(20), salaire decimal(6, 2), nuServ integer, foreign key(nuServ) references Service(nuServ) ); create table Service( ...
soukaina's user avatar
-1 votes
0 answers
33 views

Many to Many on duplicate

let's say I have item table that have {id,name,price}, user{id, name}. I made a basket table for them {id, userID, itemID, quantity}. The problem is when I add itmeA to userA. userB can not have itemA....
Ahmed Khaldoon's user avatar
0 votes
1 answer
25 views

In NestJS and TypeORM, how do I define a relation that sets relation column to NULL instead of attempting to delete the entity?

I'm using NestJS 10, PostGres 14, and TypeORM 0.3.17. If I have defined an Order entity with a relation with a User entity ... @Entity() export class Order { @PrimaryGeneratedColumn('uuid') id: ...
Dave's user avatar
  • 15.2k
0 votes
0 answers
19 views

Attribute in junction table depends on the primary key [duplicate]

I have a junction table to set up a many-to-many relationship between properties and their policies The property_id and policy_id form the primary key. As an additional attribute I wish to store a ...
Gabi23's user avatar
  • 115
-1 votes
1 answer
33 views

Cannot connect foreign key to table in SQL Server database

For all tables in the db the fk are working but for this one. CREATE TABLE Sales.SpecialOfferProduct ( SpecialOfferID INT NOT NULL, ProductID INT NOT NULL, rowguid uniqueidentifier NOT ...
taray's user avatar
  • 21
-1 votes
1 answer
59 views

Implement FK constraints as depicted in ER-diagram

While making a table, I got stuck at 2 things. We have 2 primary keys named "ID" in both Patient and Employee. And both of them are foreign keys to entity Doctor. How do I put this in ...
User's user avatar
  • 49
0 votes
0 answers
44 views

how to create a multiple ForeignKey fields in a view create and save to database

First of all, I appreciate your help and time I would like to save all the 'equipamento' fields in the database instead of just one! When I save the form, it doesn't give any errors, but looking in ...
caio oliveira's user avatar
0 votes
0 answers
7 views

How to realise relationship Django ORM model with multiple fields of another one?

In one of my projects, I came across the need to have a relationship of one model with one of several fields ** of another model**. I do not know how to do this, because if I just refer from two ...
Alexandr T's user avatar
0 votes
0 answers
16 views

Django keycloak integrated but user delete functionaliy not working , why?

I have integrated Django with Keycloak, and I’ve set the Keycloak user ID as a foreign key for the ‘django_keycloak_user’ table. When I try to delete a user from Keycloak, I encounter an error saying, ...
chavanmangesh's user avatar
1 vote
1 answer
52 views

Form the proper DBIx::Class syntax to join a table where the foreign key is stored in a one-to-one join of a different foreign table

I've been reading documentation and trying things, but I can't seem to come up with the syntax I need to create my relationship. I've simplified my need here: Table: thing Field: thingID Field: ...
Todd M's user avatar
  • 11
0 votes
2 answers
35 views

ASP.NET MVC - 2 tables, foreign key to 2nd table, Guid is present, underneath columns are null

Sorry for the 'blurry" question, but I don't know how to make it more clear. Problem is the following: I have a table with Files and another table with PaymentMethods. In Files, I have a FK ...
Glenn's user avatar
  • 3
0 votes
2 answers
74 views

how to reference foreign key in Entity when separating domain model from persistence model

I am trying out a hexagonal architecture project and separating domain model from persistence model. But I am struggling in the case when I have to reference the foreign key of another entity in the ...
hadamard's user avatar
  • 403
-1 votes
1 answer
37 views

Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'apply_ibfk_2' in the referenced table 'Job'

CREATE DATABASE Wuzzuf; USE Wuzzuf; CREATE TABLE Company_Data ( Name VARCHAR(30) NOT NULL PRIMARY KEY, City VARCHAR(15), Country VARCHAR(15) NOT NULL, ...
Abdelrahman Elaskary's user avatar
0 votes
1 answer
29 views

How to maintain data integrity in Laravel Morph relationship on database layer?

In the context of a SaaS-based system, I am designing a role management system using Laravel's Morph relationship technique. This system involves assigning roles to both company entities and admin ...
Khurram Saddique's user avatar
0 votes
0 answers
23 views

Check usage before deleting an user(Model)

I have a database, with more than 20 tables and each table has the created_by, modified_by, and deleted_by columns. I have to prevent users from deleting the entries from the Users table when it is ...
Fasna's user avatar
  • 566
0 votes
1 answer
71 views

What is the correct way to use a foreign key as a primary key in Entity Framework?

Here's the id generating entity: public class ConceptDataEntry { [Key] public long Id { get; set; } } Here's an example entity reliant on generated ids of the ConceptDataEntry entity: public ...
hamstar's user avatar
  • 141
0 votes
1 answer
29 views

How to insert values into a junction table without knowing the id of the reference table?

I have three tables: releases, dependencies and release_dependencies. Each release can have multiple dependencies which are thus store in a junction table called release_dependencies. When inserting ...
SteNafH's user avatar
0 votes
1 answer
29 views

SQLITE: referenced column's value not updated in dependent table after INSERT OR REPLACE INTO the host table

I have two SQLite tables: One has a primary id used as primary key and a secondary id which may be null (i.e. unknown yet) but if not null, it needs to be unique within the table. Another table has ...
Dmitry's user avatar
  • 3,063
0 votes
0 answers
10 views

Building relation in Django rest_framework

class Company(Timestampable, SoftDeletes): id = models.UUIDField(primary_key=True, unique=True, editable=False, default = uuid.uuid4) creator = models.ForeignKey(settings.AUTH_USER_MODEL, ...
Amith's user avatar
  • 1
0 votes
1 answer
18 views

delete and insert another table using trigger (FK key) oracle apex

I have two schedules. I want to use trigger if an appointment is deleted from the (appointments) schedule and added to (archive_appointments) CREATE TABLE "APPOINTMENTS" ( "...
Wisten's user avatar
  • 5
0 votes
0 answers
15 views

How do i connect relationships of tables on power BI manually

ERD relations, connecting 5 tables i tried doing it automatically and it works but i want to do it manually, I WANT TO SEE IF I CAN ADD RELATIONSHIPS in relation with the foreign keys and primary keys ...
Peace Dagada's user avatar
0 votes
1 answer
60 views

Foreign key constraint on two columns to restrict ALTER and INSERT to certain values

In postgres I want to restrict the possible insertion and altering of a value in a certain column to a given combination in another table. I have table A. user_id is unique, project_id is not unique. -...
four-eyes's user avatar
  • 11k
0 votes
0 answers
12 views

Django Model subset of database table

All of my models are not managed as they are based on a legacy database. There is a Partner master table holds amongst other things an ID, Code, Name and a PartnerTypeID which is a ForeignKey ...
Bruce's user avatar
  • 43
0 votes
1 answer
57 views

Define an order for a sqlite table?

I'm currently creating a sqlite database in which I need to define a ranking system. Each rank must have a unique position in the hierarchy. The first approach I thought of was to create a position ...
crazycat256's user avatar
2 votes
0 answers
77 views

Django Polymorphic behaviour (Custom ForeignKey) without using GenericForeignKey

I'm working with a legacy DB (Postgresql) that has fields like edited_by_type, and edited_by_id in many tables. This is basically a polymorphic relation for RubyOnRails persons. This represents a FK ...
user22739027's user avatar
0 votes
0 answers
34 views

Error while saving database file foreignkey constrained failed realese restore point

in shell the error I get is: OperationalError: database is locked from django.db import models from django.contrib import auth class Publisher(models.Model): name = models.CharField( ...
VOZ ESTOICA's user avatar
0 votes
1 answer
48 views

How can I filter an optional model inside include in Entity Framework Core

I'm using .NET 7 and i have the following model class: public class MachineSchedule : BaseModel { public int MachineScheduleId { get; set; } public EWeekDay WeekDay { get; set; } ...
guirms's user avatar
  • 260
1 vote
1 answer
23 views

Android - What is ForeignKey in crossref table used to?

My app defines two entities CardEntity and TagEntity, these entities have a many-to-many relationship. I have implemented the following cross ref table to model the many-to-many relationship: @Entity( ...
eqtèöck's user avatar
  • 1,013
1 vote
2 answers
45 views

Postgres 11 not enforcing foreign key constraint?

Our PostgreSQL 11 database has tables A and B. postgres=# \d A; Table "public.A" Column | Type | Collation | Nullable | ...
Mate Varga's user avatar
  • 3,226
0 votes
2 answers
44 views

Correct way to approach composite foreign key

I have multiple classes X, Y, Z: public class X { public int Id { get; set; } public virtual List<Image> Images { get; set; } } public class Y { public int Id { get; set; } public ...
AkshatSparrow's user avatar
1 vote
1 answer
38 views

check foreign key constraint order in postgresql

consider i have a table like lgs_item which has following items id title code -------------------------------- 1 laptop 01 and i have several tables which referenced to ...
Amir Hesari's user avatar
-1 votes
2 answers
96 views

mysql - add foreign key constraint to a table

I have two tables name users , user_friends. user has over 10 million records and user_friends has more then 600 million records. The problem is that it does not have a constraint . i.e when a user is ...
Engr.Aftab Ufaq's user avatar
0 votes
0 answers
26 views

Bidirectional relationship OneToMany in Java Entity

I am working with Jakarta (JPA) and postgres. I use DBeaver as a database manager. I have two entities defined, teacher and subject. The relationship between them is, a teacher can teach several ...
Talenel's user avatar
  • 472
0 votes
1 answer
37 views

Entity Framework foreign key is null

When trying to create an Edificio that contains a relationship with another table (Facultad) in the database, it does not allow it to be stored and the EdiFac virtual public foreign key in model ...
f_dippiton's user avatar

1
2 3 4 5
227