foreign-keys

Storing “redundant” foreign keys to avoid joins

纵然是瞬间 提交于 2021-02-08 10:12:27
问题 I'm designing a database for a web application project and I came to the conclusion I may have few queries that will require a lot of joined tables to just make one check. I'm wondering how bad is it to store the foreign key somewhere on the way to decrease number of joins required for these queries? To give you an example of what I have at this moment: Service => Booking => Transaction => Wallet => BonusOffer I need to check whether the service has been bought with wallet associated with a

Changing a primary key to a composite primary key

橙三吉。 提交于 2021-02-08 09:41:34
问题 I've been using a "normal" (non-composite) primary key for one of my tables. Now I want to change it to a composite primary key. My tables look something like this: -- Table 1 CREATE TABLE foo ( id SERIAL PRIMARY KEY, id2 INT, ... ) -- Table 2 CREATE TABLE bar ( id SERIAL PRIMARY KEY, id_foo INT REFERENCES foo (id) ) The problem here is that psql does not want to drop the old primary key, since other tables reference it. Is there any way of getting around this without dropping the whole

Is there a safe way to modify the table pg_constraint so as no more checking be done ( temporarily )?

旧巷老猫 提交于 2021-02-08 06:11:56
问题 So as to restore a database from a dump, I want to temporarily disable all the constraints in all the tables of my database, which content I have emptied ( postregsql 8.3). Is there a safe way to modify a field in the pg_constraint table that easily allows to bypass the constraint ? I don't think so when I look at the documentation pg_constraint. Is it possible to drop the pg_constraint table content, then refill it again ? If not, how people do restore/copy databases with tables that are

Is there a safe way to modify the table pg_constraint so as no more checking be done ( temporarily )?

橙三吉。 提交于 2021-02-08 06:11:44
问题 So as to restore a database from a dump, I want to temporarily disable all the constraints in all the tables of my database, which content I have emptied ( postregsql 8.3). Is there a safe way to modify a field in the pg_constraint table that easily allows to bypass the constraint ? I don't think so when I look at the documentation pg_constraint. Is it possible to drop the pg_constraint table content, then refill it again ? If not, how people do restore/copy databases with tables that are

EF Core 2.2 - Two foreign keys to same table

不打扰是莪最后的温柔 提交于 2021-02-08 05:42:18
问题 I have a similar problem to the one posted here: Entity Framework Code First - two Foreign Keys from same table, however it's very old and doesn't apply to Core and I can't get the suggestions to work for me. Basically, I'm trying to create a fixture table which will have two foreign keys to the team table. A fixture is made up of a home team and an away team. Having nullable fields isn't an option. Consider a fixture, with two teams. public class Fixture { public int Id { get; set; } public

Specifying Foreign Key Entity Framework Code First, Fluent Api

不打扰是莪最后的温柔 提交于 2021-02-07 12:51:36
问题 I have a question about defining Foreign Key in EF Code First Fluent API. I have a scenario like this: Two class Person and Car. In my scenario Car can have assign Person or not (one or zero relationship). Code: public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class Car { public int Id { get; set; } public string Name { get; set; } public Person Person { get; set; } public int? PPPPP { get; set; } } class

Specifying Foreign Key Entity Framework Code First, Fluent Api

微笑、不失礼 提交于 2021-02-07 12:50:26
问题 I have a question about defining Foreign Key in EF Code First Fluent API. I have a scenario like this: Two class Person and Car. In my scenario Car can have assign Person or not (one or zero relationship). Code: public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class Car { public int Id { get; set; } public string Name { get; set; } public Person Person { get; set; } public int? PPPPP { get; set; } } class

Setting constraint deferrable doesn't work on PostgreSQL transaction

喜你入骨 提交于 2021-02-07 11:31:47
问题 This is the situation: I have two tables where the one references the other (say, table2 references table1). When creating these tables, I did set the foreign key constraint as DEFERRABLE and the ON UPDATE and ON DELETE clauses as NO ACTION (which is the default). But still, when running the transaction below, I get the following error. Transaction: START TRANSACTION; SET CONSTRAINTS ALL DEFERRED; UPDATE table1 SET blah blah; UPDATE table2 SET blah blah; COMMIT; Error: ERROR: update or delete

Error Code: 1822 when data types are matching, with composite key

╄→尐↘猪︶ㄣ 提交于 2021-02-05 10:56:13
问题 Getting an Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'subject_ibfk_1' in the referenced table 'enrolment' when attempting to create the subject table. The problem is, the error does not occur on the previous table student . The data types are the same, and the primary keys are defined. This error occurs for both the enrolment and grade tables. create table enrolment( stud_id char(9) not null, subj_code char(8) not null, semester tinyint unsigned

Postgres unique combination constraint across tables

陌路散爱 提交于 2021-02-05 07:45:26
问题 I have three tables - file ( file_id int primary key filename text not null etc... ) product ( product_id int primary key etc.... ) product_attachment ( product_id references product file_id references file ) I want to ensure that when these are natural-joined, product_id + filename is unique. The best solution I have so far involves adding filename to the product_attachment table, but I'm wondering if there's a way to avoid that. 回答1: If the filename column is not unique you can add a custom