class Monolithic(models.Model):
facepng_id = models.IntegerField(blank=True, null=True)


facepng_id as Integer not ForeignKey?


> UPDATE items_monolithic SET facepng_id=items_monolithic.id FROM 
items_monolithic INNER JOIN items_facepng ON 
items_monolithic.object=items_facepng.obj ;
Error: near "from": syntax error
 
I'll keep working on the code.

Add a facepng_id Integer Field to Monolith. Run makemigrations. Add a 
> RunSQL command to the new migrations file (
> https://docs.djangoproject.com/en/2.2/ref/migration-operations/#runsql).  
> Something like this:
>
> Update items_monolith SET facepng_id=items_monolith.id FROM 
> items_monolith INNER JOIN items_facepng ON 
> items_monolith.object=items_facepng.obj 
>
>  
>
> Then delete the object and obj fields from the models. Run makemigrations 
> again. Verify that your app doesn’t reference those fields anywhere else.
>
>  
>
> Then you could access facepng like so:
>
> monolithic.facepng_set
>
>  
>
> Of course, this is just a very rough idea of what to do, and I’m not sure 
> what the whole structure of your tables is.
>
>  
>
> *From:* [email protected] <javascript:> [mailto:
> [email protected] <javascript:>] *On Behalf Of *[email protected] 
> <javascript:>
> *Sent:* Monday, May 6, 2019 11:28 AM
> *To:* Django users
> *Subject:* ORM help with INNER JOIN and GROUP BY
>
>  
>
> I've inherited an application written django 1.11. The old application 
> uses raw() with INNER JOIN and GROUP BY. 
>
>  I cannot seem to figure out how to do inner join and group by properly 
> the ORM way.
>
>  
>
> The raw() query is below.
>
>  
>
>             SELECT  * FROM items_monolithic
>
>             INNER JOIN items_facepng
>
>             ON items_monolithic.object == items_facepng.obj
>
>             GROUP BY items_monolithic.object
>
>             ORDER BY object ASC
>
>  
>
>  
>
> Things kind of work with raw() but that doesn't feel right. And I get 
> nasty warnings about RawQuerySet not supporting certain things when I try 
> to use the query set that is returned.
>
>  
>
> From what I understand every monolithic object has 1 or more faces 
> (graphic/picture).
>
>  
>
> I would call the monolithic a one-to-many relationship with facepng but I 
> see django calls this a ForeignKey.
>
>  
>
> I'm working with these models (yes, a field named object is "bad", it's 
> what I was given)
>
>  
>
>  
>
> class Monolithic(models.Model):
>
>    object = models.CharField(max_length=128, blank=False, null=False, 
> unique=True)
>
>  
>
> class FacePng(models.Model):
>
>     obj = models.CharField(max_length=128, blank=True, null=True)
>
>  
>
>  
>
> I do not see the ForeignKey relationship between Monolithic and FacePng. 
>
>  
>
> Changing Monolithic class to models.ForeignKey() breaks lots of things.
>
>  
>
> So I'd prefer to figure out how to do the inner join and group by query 
> but if that's not the django way and I need to change Monolithic.objects to 
> a ForeignKey() and fix all the stuff that is broken I can do that that too. 
>
>  
>
> Just need some guidance on how to proceed.
>
>  
>
>  
>
>  
>
> -- 
> 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 [email protected] <javascript:>.
> To post to this group, send email to [email protected] 
> <javascript:>.
> 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/ab7ecf5b-9ae8-4428-9502-6b7d5dec03b5%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/ab7ecf5b-9ae8-4428-9502-6b7d5dec03b5%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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 [email protected].
To post to this group, send email to [email protected].
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/7af29fa6-5cfc-4bda-a471-ed8ee26b37ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to