foreign-keys

Entity framework update foreign key to null without query and without id of the related entity

一笑奈何 提交于 2019-12-12 03:25:19
问题 Using EF 6.1, I want to set a foreign key to null (break the relation between two entities), without querying for either object first. Preferably, I also don't want to supply the id of the related entity (since that requires adding it to my HTML). My current code doesn't do unnecessary queries, but requires the id of the related entity: public void RemoveCurrentTeacherOfGroup(int groupId, int teacherId) { var group = new Group { Id = groupId }; var teacher = new Teacher { Id = teacherId };

mysql foreign key “permissions” on delete

那年仲夏 提交于 2019-12-12 03:14:19
问题 I'm working on a little support (ticket) system. My tables are tickets and ticket_replies. Design of tickets table is id|user_id|title|... Design of ticket_replies looks like: id|ticket_id|... The foreign key I added looks like this: ALTER TABLE `ticket_replies` ADDFOREIGN KEY (`ticket_id`) REFERENCES `sampleauth`.`tickets`(`id`) ON DELETE CASCADE ON UPDATE CASCADE; Now when I delete a "ticket" in the "ticket" table it gets deleted in "ticket_replies" too. The other way this doesn't work, all

Asp.net mvc entity framework code first associate foreign key with different name

天大地大妈咪最大 提交于 2019-12-12 03:09:24
问题 How can I associate a foreign key with different names here: createdby in post and UserID in users table. public class Post : IValidatableObject { [Key] public long PostID { get; set; } public long? ParentPostID { get; set; } [ForeignKey("CreatedBy")] public virtual User Users { get; set; } [ScaffoldColumn(false)] public DateTime? CreatedDate { get; set; } [ScaffoldColumn(false)] public long? CreatedBy { get; set; } } public class User { [Key] public long UserID { get; set; } [Required]

CakePHP Can't insert record with foreign key error

坚强是说给别人听的谎言 提交于 2019-12-12 02:57:09
问题 I'm getting the following error when trying to insert a record into the table: Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( invoice . quotes , CONSTRAINT quotes_ibfk_1 FOREIGN KEY ( contacts_id ) REFERENCES Contacts ( id )) I have a relationship setup between two tables 'contacts' and 'quotes'. 'quotes' has a foreign key set up contacts_id. The Add method in my quotes controller looks like this: public function

One of Composite primary key as Foreign key Mysql

拈花ヽ惹草 提交于 2019-12-12 02:53:26
问题 I Have two Tables. CREATE TABLE One( Oneid int, Twoid int, data char(20), PRIMARY KEY(Oneid,Twoid) ) Table One is Oneid and Twoid as primary key. CREATE TABLE Two( Twoid int, data char(20), PRIMARY KEY(Twoid) ) And I want to One.Twoid is foreign key for Table Two. How to solve it. Thank a lot. 回答1: Add the constraint in the CREATE TABLE statement: CREATE TABLE Two( Twoid int, data char(20), PRIMARY KEY (Twoid)); CREATE TABLE One( Oneid int, Twoid int, data char(20), PRIMARY KEY (Oneid,Twoid),

Introducing FOREIGN KEY may cause cycles or multiple cascade paths

孤街醉人 提交于 2019-12-12 02:49:26
问题 i am using Entity Framework with code first approach. In my onModelCreating I am creating my tables with keys and relationships (I am using Fluent API approach, not data annotations). But when I try to generate my model using Update-Database command I receive following error Introducing FOREIGN KEY constraint 'FK_customers.invoices_customers.billingCenters_billingCenterId' on table 'invoices' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or

Foreign key with many-to-many containment constraint

≡放荡痞女 提交于 2019-12-12 02:47:34
问题 Quick question that I'm having trouble putting into search terms: Suppose I have a many-to-many relationship between players and teams: CREATE TABLE players ( id bigserial primary key, name text not null ); CREATE TABLE teams ( id bigserial primary key, name text not null, team_captain_id bigint not null references players(id) ); CREATE TABLE team_players ( id bigserial primary key, player_id bigint not null, team_id bigint not null ); ALTER TABLE team_players ADD CONSTRAINT uq_team_players

Output of the SQLite's foreign_key_list pragma

Deadly 提交于 2019-12-12 02:43:27
问题 Using SQLite3 with the following schema: CREATE TABLE Customers(ID INTEGER PRIMARY KEY, Company TEXT NOT NULL UNIQUE, Country TEXT NOT NULL, City TEXT NOT NULL); CREATE TABLE Orders(ID INTEGER PRIMARY KEY, CustomerID INTEGER NOT NULL, FOREIGN KEY(CustomerID) REFERENCES Customers(ID) ON DELETE RESTRICT ON UPDATE RESTRICT); and issuing this command: PRAGMA foreign_key_list(Orders); results in the following output: 0|0|Customers|CustomerID|ID|RESTRICT|RESTRICT|NONE As the documentation says

Get all referencing columns to a referenced column in linq

前提是你 提交于 2019-12-12 02:27:12
问题 Say you have a Table named Sales with a SalesOrderId. Then you have two tables SalesOrderHeader and SalesReport that reference SalesOrderId. Starting from the Sales Tables and SalesOrderId, is there any way to figure out those two table reference it using linq? I want to add a Debug.Assert to my code so that I can update that piece of code whenever there is another foreign key constraint added against a column. 回答1: On the database side, these might help: SELECT * FROM INFORMATION_SCHEMA

Using one of the columns in a composite key as a foreign key

感情迁移 提交于 2019-12-12 02:15:46
问题 I was trying to see whether I can use one of the columns in a composite key as a foreign key. And I got strange result. CREATE TABLE TESTPARENT( PK1 INT, PK2 INT, PRIMARY KEY(PK1,PK2) ); Query OK, 0 rows affected (0.01 sec) CREATE TABLE TESTCHILD1( FK1 INT, FOREIGN KEY (FK1) REFERENCES TESTPARENT(PK1) ); Query OK, 0 rows affected (0.01 sec) CREATE TABLE TESTCHILD2( FK2 INT, FOREIGN KEY (FK2) REFERENCES TESTPARENT(PK2) ); ERROR 1005 (HY000): Can't create table 'test.TESTCHILD2' (errno: 150)