foreign-keys

MySQL: Two foreign keys in one table referring to another table

主宰稳场 提交于 2019-12-02 03:44:20
问题 I've come across something that seemed simple before but has me scratching my head again. I have a table for users: user_id (PK) | username| email | something ... and a table for "views" for when one user has viewed another user: view_id (PK) | viewer_id | viewed_id | view_date The "viewer_id" and "viewed_id" are both user_ids, allowing me to search separately for instances when a user was the viewer or the one being viewed. I initially thought that both of these columns would be foreign keys

Programmatically obtain Foreign keys between POCOs in Entity Framework 6

[亡魂溺海] 提交于 2019-12-02 03:02:57
问题 I am faced with an EF6 Code First context, with a few DbSet s of POCOs that have navigation properties (and foreign keys) between them, e.g.: public partial class Person { public Guid Id { get; set; } public virtual ICollection<Address> Address { get; set; } } public partial class Address { public Guid Id { get; set; } public Guid FK_PersonId { get; set; } public virtual Person Person { get; set; } } modelBuilder.Entity<Person>() .HasMany (e => e.Address) .WithRequired (e => e.Person)

Foreign key for multiple tables and columns?

微笑、不失礼 提交于 2019-12-02 02:56:44
I have been learning about Foreign keys and I wanted to know if the way I used it is correct in the example below: CREATE TABLE user( id INT(11) NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(20) NOT NULL, PRIMARY KEY (id) ); CREATE TABLE items( i_id INT(11) NOT NULL AUTO_INCREMENT, name TINYTEXT NOT NULL, price DECIMAL(8,2) NOT NULL, PRIMARY KEY (i_id) ); CREATE TABLE user_purchase( i_id INT(11) NOT NULL, name TINYTEXT NOT NULL, id INT(11) NOT NULL, FOREIGN KEY (i_id) REFERENCES items(i_id), FOREIGN KEY (name) REFERENCES items(name), FOREIGN KEY (id) REFERENCES user

Error 1452 MySQL

依然范特西╮ 提交于 2019-12-02 02:05:52
问题 Inserting data into an empty table, but got error 1452. I am not sure why MySQL mentions the NameInfo table within the error. CREATE TABLE NameInfo ( Language VARCHAR(7) NOT NULL, Status VARCHAR(13) NOT NULL, Standard VARCHAR(13) NOT NULL, Name VARCHAR(13) NOT NULL, Name_ID INT(4) NOT NULL, Place_ID INT(9) NOT NULL, Supplier_ID INT(4) NOT NULL, Date_Supplied DATE NOT NULL, PRIMARY KEY (Name_ID), FOREIGN KEY (Supplier_ID) REFERENCES Supplier(Supplier_ID), FOREIGN KEY (Place_ID) REFERENCES

Order of deletion with foreign key constraints,

心已入冬 提交于 2019-12-02 01:14:32
I have a schema with three tables and foreign key 'On Delete' constraints as below: | -> FK (cascade) -> | Organisation | | Users | - FK (cascade) Categories -> FK(restrict) -> | If I delete an organisation I want to delete the users and the categories related to it, but I can't allow a category to be deleted if a user refers to it except in the case where the whole organisation is being deleted. At present if I delete an organisation the category deletion fails if there's a user referring to it. This seems to indicate that MySQl is processing the foreign key constraints on the Categories

What is an easy way to list the foreign key contraints in an MDB?

自闭症网瘾萝莉.ら 提交于 2019-12-02 01:08:06
问题 What is an easy way to list the foreign key contraints in an MDB? Is there a system table that can be queried in order to list this information? Specifically, I need to know whether any foreign key contraints exist in the MDB. 回答1: Take a look at the results of select * from MSysRelationships . 回答2: Or you can examine the relationships collection of the database object: Public Sub PrintRelationships() Dim varItem As Variant Dim varItem2 As Variant For Each varItem In CurrentDb.Relations Debug

MySQL: Two foreign keys in one table referring to another table

混江龙づ霸主 提交于 2019-12-02 00:54:45
I've come across something that seemed simple before but has me scratching my head again. I have a table for users: user_id (PK) | username| email | something ... and a table for "views" for when one user has viewed another user: view_id (PK) | viewer_id | viewed_id | view_date The "viewer_id" and "viewed_id" are both user_ids, allowing me to search separately for instances when a user was the viewer or the one being viewed. I initially thought that both of these columns would be foreign keys, but having created the tables in my schema.yml file (I'm using Doctrine 1.2) and specified two

Django queries: Count number of objects with FK to model instance

徘徊边缘 提交于 2019-12-02 00:44:03
问题 This should be easy but for some reason I'm having trouble finding it. I have the following: App(models.Model): ... Release(models.Model): date = models.DateTimeField() App = models.ForeignKey(App) ... How can I query for all App objects that have at least one Release? I started typing: App.objects.all().annotate(release_count=Count('??????')).filter(release_count__gt=0) Which won't work because Count doesn't span relationships, at least as far as I can tell. BONUS: Ultimately, I'd also like

Deleting from a MySQL table with foreign key constraints

岁酱吖の 提交于 2019-12-02 00:04:36
问题 I have two tables, with some data loaded in it and the two reference each other. Table B references the primary key of Table A. I manually attempt to delete some table rows from Table A that are present in Table B, and I get this: #1451 - Cannot delete or update a parent row: a foreign key constraint fails ( TableA . TableB , CONSTRAINT TableB_ibfk_2 FOREIGN KEY ( column ) REFERENCES flashcard ( primaryKeyColumn )) I am not quite sure what's up here. From what I understand,if I delete some

Programmatically obtain Foreign keys between POCOs in Entity Framework 6

不羁的心 提交于 2019-12-01 23:22:39
I am faced with an EF6 Code First context, with a few DbSet s of POCOs that have navigation properties (and foreign keys) between them, e.g.: public partial class Person { public Guid Id { get; set; } public virtual ICollection<Address> Address { get; set; } } public partial class Address { public Guid Id { get; set; } public Guid FK_PersonId { get; set; } public virtual Person Person { get; set; } } modelBuilder.Entity<Person>() .HasMany (e => e.Address) .WithRequired (e => e.Person) .HasForeignKey (e => e.FK_PersonId) .WillCascadeOnDelete(false); Given these types, is there any proper way (i