Saturday, January 2, 2016

Re: I'm missing something with Models and database migrations

Thank you both.

--M

On Monday, December 28, 2015 at 1:09:08 AM UTC-6, Peter of the Norse wrote:
OK.  Let's take a look at the documentation.  Here is what you are looking for:

class Invoice(models.Model):
    invoice_date = models.DateField('created date', auto_now=True)
    line_item_set = models.ManyToManyField(LineItem, through='InvoiceLineItem')

class LineItem(models.Model):
    descr = models.CharField(max_length=100)
    cost = models.DecimalField(max_digits=5, decimal_places=2)

class InvoiceLineItem(models.Model):
    created_dt = models.DateField(auto_now=True)
    invoice = models.ForeignKey(Invoice)
    line_item = models.ForeignKey(LineItem)

When you use through with a ManyToManyField, it doesn't create an extra table; otherwise, it does.  Also, I've cleaned up things so that they match Django standards.


On Dec 22, 2015, at 10:33 PM, Michael Molloy <ukal...@gmail.com> wrote:

I figured it out, again with help from Vijay. Thanks for getting me thinking in the right direction.

So here is my cross reference model:

class Invoice_Line_Items(models.Model):
 created_dt
= models.DateField('created date', auto_now=True)
 invoice
= models.ManyToManyField(Invoices)
 line_item
= models.ManyToManyField(Line_Items)



I was incorrectly looking for a single table named Invoice_Line_Items because I didn't understand how Django creates multiple tables to deal with ManyToMany relationships like this. The table I was expecting was there, but it didn't have columns for invoice IDs or line_item IDs. Django actually created these three tables to handle this:

gpga_invoice_line_items

gpga_invoice_line_items_invoice

gpga_invoice_line_items_line_item  


Thanks again.
--M      

On Tuesday, December 22, 2015 at 8:49:10 PM UTC-6, Michael Molloy wrote:
Still confused. I think I still need to create the cross reference Django model myself, and then use makemigrations to turn it into a table.

I need a place to store invoices and associated line items, so if the model code I put in my first post is incorrect, what should it look like? 

--Michael

On Tuesday, December 22, 2015 at 8:29:36 PM UTC-6, Michael Molloy wrote:
I was trying to create the cross reference table myself. I'm coming at this from a java/oracle background where I would create two tables (invoices and line_items), and then a third (invoice_line_item_xref) that would contain invoice IDs and associated line item IDs. I think you just helped me see my misunderstanding of how Django does things.

So, in the list of tables (now that I know what I'm looking for), I see a table called invoice_line_items_xref_invoice_id and another called invoice_line_items_xref_line_item_id. Those must be the two tables that Django created to handle the many to many relationship. Is that correct? 

--Michael 

On Tuesday, December 22, 2015 at 6:29:51 PM UTC-6, Vijay Khemlani wrote:
You have two pairs of fields with the same name (line_item_id and invoice_id), what are you trying to do exactly? Why the IntegerFields?

In a Many To Many relation the columns are added to a third table between the models.

On Tue, Dec 22, 2015 at 9:20 PM, Michael Molloy <ukal...@gmail.com> wrote:
Python 3.3 and Django 1.8 running on Openshift with a Postgresql database

I'm trying to set up an Invoices table, a Line_Item table, and a cross reference between them. Here are the relevant models:


class Invoices(models.Model):
 invoice_date
= models.DateField('created date', auto_now=True)

class Line_Items(models.Model):
 descr
= models.CharField(max_length=100)
 cost
= models.DecimalField(max_digits=5, decimal_places=2)

class Invoice_Line_Items_Xref(models.Model):
 created_dt
= models.DateField(auto_now=True)
 invoice_id
= models.IntegerField(default=0)
 line_item_id
= models.IntegerField(default=0)
 invoice_id
= models.ManyToManyField(Invoices)
 line_item_id
= models.ManyToManyField(Line_Items)



I don't think the syntax for the cross reference table above is correct, but it is one of the permutations that I've tried. The layout above resulted in this migration after running makemigrations

operations = [
 migrations
.AddField(
 model_name
='invoice_line_items_xref',
 name
='invoice_id',
 field
=models.ManyToManyField(to='gpga.Invoices'),
 
),
 migrations
.AddField(
 model_name
='invoice_line_items_xref',
 name
='line_item_id',
 field
=models.ManyToManyField(to='gpga.Line_Items'),
 
),
]



However, when I push the code to Openshift, even though the migration runs against the database, the invoice_id and line_item_id columns do not appear on the database table.

I have no idea what I'm doing wrong. Thank you for any help.

--Michael

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9953458c-908d-4376-89f9-eb68e37d527a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/848c0bba-993a-4599-9829-3c8081af32ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter of the Norse



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87f98ae6-c5fe-45ff-8bd8-92435e6a27ea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment