foreign-keys

has_many :through with a foreign key?

元气小坏坏 提交于 2019-12-18 10:35:28
问题 I've read multiple questions about this, but have yet to find an answer that works for my situation. I have 3 models: Apps , AppsGenres and Genres Here are the pertinent fields from each of those: Apps application_id AppsGenres genre_id application_id Genres genre_id The key here is that I'm not using the id field from those models. I need to associate the tables based on those application_id and genre_id fields. Here's what I've currently got, but it's not getting me the query I need: class

What is a proper naming convention for MySQL FKs?

百般思念 提交于 2019-12-18 09:57:25
问题 Being that they must be unique, what should I name FK's in a MySQL DB? 回答1: In MySQL, there is no need to give a symbolic name to foreign key constraints. If a name is not given, InnoDB creates a unique name automatically. In any case, this is the convention that I use: fk_[referencing table name]_[referenced table name]_[referencing field name] Example: CREATE TABLE users( user_id int, name varchar(100) ); CREATE TABLE messages( message_id int, user_id int ); ALTER TABLE messages ADD

How to update MongoDB documents with arrays of sub-documents

和自甴很熟 提交于 2019-12-18 09:33:49
问题 I am working with a MongoDB database whose collections model classes , students , subjects , and [academic] performance . Below are the Mongoose based schema and models: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; // SUBJECT collection var subjectSchema = new Schema({ name : { type: String, required: true }, category : { type: String, required: true } }); var Subject = mongoose.model('Subject', subjectSchema); // STUDENT collection

Reference to composite primary key in Access 2007

倖福魔咒の 提交于 2019-12-18 08:50:30
问题 I have looked around and found some questions similar but they were for SQL Server instead. Here is a small database structured I have create just to show you the relationships I want to model. Basically it's quite simple, each year has 12 periods and an instance of period-year cannot occur twice (period 9 year 2012 cannot occur more than once ever). So I thought that the best way to model this would be to have a table period with only one field with values from 1-12, a table year following

Handling foreign key exceptions in PHP

天大地大妈咪最大 提交于 2019-12-18 05:59:14
问题 What is the best way in PHP to handle foreign key exceptions on a mysql database? Is there a mysql class that can be used to simplify any code? Ideally, what I want to do, as an example, is to try to delete a record where it is the foreign key parent to any number of child tables. The foreign key throws the exception, so then I would like to be able to look at each foreign key table and test it, giving meaningful feedback on the tables and number of records causing the exception. This would

Entity Framework 0..1 to 0 relation

我怕爱的太早我们不能终老 提交于 2019-12-18 05:10:23
问题 class First { [Key] public int Id { get; set; } } class Second { [Key] public int Id { get; set; } public int? First_Id { get; set; } [ForeignKey("First_Id")] public First First { get; set; } } public class SecondMapping : EntityTypeConfiguration<Second> { public SecondMapping () : base() { this.HasOptional(s => s.First) .With ... ??? } } Second may have a reference to First. But First never has a reference to Second. Is it possible to apply this mapping with Entity Framework 4.1? EDIT:

Many-to-many relations in RDBMS databases

百般思念 提交于 2019-12-18 05:07:09
问题 What is the best way of handling many-to-many relations in a RDBMS database like mySQL? Have tried using a pivot table to keep track of the relationships, but it leads to either one of the following: Normalization gets left behind Columns that is empty or null What approach have you taken in order to support many-to-many relationships? 回答1: Keep track of a many-to-many relationship in a table specifically for that relationship (sometimes called a junction table ). This table models the

Can you have a Foreign Key onto a View of a Linked Server table in SQLServer 2k5?

瘦欲@ 提交于 2019-12-18 04:19:12
问题 I have a SQLServer with a linked server onto another database somewhere else. I have created a view on that linked server create view vw_foo as select [id], [name] from LINKEDSERVER.RemoteDatabase.dbo.tbl_bar I'd like to to the following alter table [baz] add foo_id int not null go alter table [baz] with check add constraint [fk1_baz_to_foo] foreign key([foo_id]) references [dbo].[vw_foo] ([id]) go But that generates the error: "Foreign key 'fk1_baz_to_foo' references object 'dbo.vw_foo'

Deleting a row with a self-referencing foreign key

[亡魂溺海] 提交于 2019-12-18 03:56:28
问题 I have a MySQL table whose definition is as follows: CREATE TABLE `guestbook` ( `Id` int(10) unsigned NOT NULL, `ThreadId` int(10) unsigned NOT NULL, PRIMARY KEY (`Id`), KEY `ThreadId` (`ThreadId`), CONSTRAINT `guestbook_ibfk_1` FOREIGN KEY (`ThreadId`) REFERENCES `guestbook` (`Id`) ) ENGINE=InnoDB; and currently there's only 1 row in the table: mysql> select * from guestbook; +-----+----------+ | Id | ThreadId | +-----+----------+ | 211 | 211 | +-----+----------+ The problem is that there's

Django: getting the list of related records for a list of objects

牧云@^-^@ 提交于 2019-12-18 03:43:23
问题 I have two models related one-to many: class Person(models.Model): name = models.CharField(max_length=255); surname = models.CharField(max_length=255); age = models.IntegerField(); class Dog(models.Model): name = models.CharField(max_length=255); owner = models.ForeignKey('Person'); I want to output a list of persons and, below each person a list of dogs he has. Here's how I can do it: in view: persons = Person.objects.all()[0:100]; in template: {% for p in persons %} {{ p.name }} has dogs: