foreign-key-relationship

How to delete automatically all reference rows if parent row get deleted in mysql?

匆匆过客 提交于 2019-11-28 08:44:44
问题 I have a database which contains around 50 tables. Suppose I have a table named parent with id primary key and 24 approx child tables with reference to this parent table. I haven't used on delete cascade. I have already searched about doing joins can perform delete in all child table. But join on 20-30 tables? Its too much. Please let me know is there any other solution to delete all this child rows if parent get deleted. 回答1: You can do with ON DELETE CASCADE . ALTER TABLE childTable ADD

Rails: Scaffold to automatically do one-to-many relationship

眉间皱痕 提交于 2019-11-28 08:25:39
Not sure if I'm reading this right, but it seems like Scaffold will not do a one-to-many relationship in its entirety. For instance, if I create messages with scaffold and then I want comments on those messages (one message -> many comments ), I have to go through and change everything. For instance, I have to change this in the comment 's new view <% form_for(@comment) do |f| %> to this <% form_for([@message, @comment]) do |f| %> and then change the Action to set up the @message var... amongst other things. This cannot currently be done automatically with Scaffold, right? This is true, but,

How to I show a list of ForeignKey reverse lookups in the DJango admin interface?

不羁岁月 提交于 2019-11-28 07:31:46
I have a couple of models: class Customer(models.Model): customer_name = models.CharField(max_length=200) def __unicode__(self): return self.customer_name class Meta: ordering = ('customer_name',) class Unit(models.Model): unit_number = models.IntegerField() rentable = models.BooleanField() owner = models.ForeignKey(Customer, related_name='units', blank=True, null=True) def __unicode__(self): return str(self.unit_number) class Meta: ordering = ('unit_number',) I have the admin interface working fine when I'm adding a unit (I can select which customer to assign it to) but when I go to create

Django foreign key relation in template

[亡魂溺海] 提交于 2019-11-28 06:53:42
i know you will say that this question is asked before many times but i havent solved it yet... models.py class Doc(UploadModel): doc_no = models.CharField(max_length=100, verbose_name = "No", blank=True) date_added = models.DateTimeField(verbose_name="Date", default=datetime.now, editable=False) class DocImage(models.Model): property = models.ForeignKey(Doc, related_name='images') image = FileBrowseField("Docs", max_length=200, directory="doc_img/%Y/%m/%d/%H/%M/%S/", extensions=[".jpg",".tif"], blank=True, null=True) views.py def doc_detail(request, dosc_no): res = Doc.objects.filter(doc_no =

Foreign key referencing a 2 columns primary key in SQL Server

狂风中的少年 提交于 2019-11-28 06:18:10
This question is pretty much similar to this one , but for SQL Server 2005 : I have 2 tables in my database: --'#' denotes the primary key [Libraries] #ID #Application Name 1 MyApp Title 1 2 MyApp Title 2 [Content] #ID Application LibraryID Content 10 MyApp 1 xxx 11 MyApp 1 yyy (the database is obviously much more complex and having this double key makes sense) Each library is identified by its unique ID and Application name. I'm trying to ensure that each content is properly referencing an existing library. When creating the constraint (using the Wizard) as Primary key table Foreign key table

Does Rails need database-level constraints?

故事扮演 提交于 2019-11-27 19:45:32
I have the same problem as in the following post . So I am wondering, why doesn't Rails support generating foreign keys by default? Isn't it necessary? Or are we supposed to do it manually? Database constraints aren't required any more than wearing seat-belts are required in your car. You can drive around all you like and everything will work great until a problem arrives. The seat-belt (constraints) keep you (the data) safe. So it's highly recommended that you create constraints to enforce data-integrity at the database level, because it's highly likely that 1) You will interact with the

POSTGRESQL Foreign Key Referencing Primary Keys of two Different Tables

你离开我真会死。 提交于 2019-11-27 18:50:52
I have two tables Books and Audiobooks, both of which have ISBN as their primary keys. I have a table writtenby that has an isbn attribute that has a foreign key constraint to Books and Audiobooks ISBN. The issue that comes up when I insert into writtenby is that postgresql wants the ISBN I insert into writtenby to be in both Books and Audiobooks. It makes sense to me to have a table writtenby that stores authors and the books/audiobooks they have written, however this does not translate to a table in postgresql. The alternative solution I am thinking of implementing was having two new

Django: list all reverse relations of a model

徘徊边缘 提交于 2019-11-27 16:05:50
问题 I would like my django application to serve a list of any model's fields (this will help the GUI build itself). Imagine the classes (ignore the fact that all field of Steps could be in Item , I have my reasons :-) ) class Item(models.Model): name = models.CharField(max_length=100) description = models.TextField() class Steps(models.Model): item = models.OneToOneField('Item', related_name='steps') design = models.BooleanField(default=False) prototype = models.BooleanField(default=False)

The better way to pass the foreign_key value to the Rails controller

半城伤御伤魂 提交于 2019-11-27 15:20:44
It's been almost a week since I've began to dig deeper in forms , associations , hashes , symbols... But it seems I cannot solve the puzzle without your help . I am working on a project for displaying different galleries content . The basic idea is when the user sees the names of galleries (names are links ) to be able to click on chosen one. Then all the images ,that belong to this gallery , are displayed . On the bottom there should be a link "Add image in this gallery" . My models : class Gallery < ActiveRecord::Base attr_accessible :name has_many :pictures end class Picture < ActiveRecord:

Any example of a necessary nullable foreign key?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 12:49:13
Customers customer_id Orders order_id customer_id fk If I have two tables and define a foreign key on customer_id in the Orders table, by allowing it to be null I am saying that I can have an order that does not have a customer associated with it. As such, the notion of a nullable foreign key seems at odds with the purpose of a foreign key, which is to enforce this constraint. Is there a simple example of a situation in which a nullable foreign key would be necessary? Or an argument in favor of permitting them? Imagine a table that holds the TODOs of a team. If a TODO is not yet assigned to a