Simple Join with no matching primary keys

38 views
Skip to first unread message

aysh...@gmail.com

unread,
Mar 7, 2017, 1:50:24 PM3/7/17
to Django users
Greetings,

I am trying to do a join on the following 2 models (tables)

class Rio1(models.Model):
datestr = models.DateTimeField()
source = models.TextField()
dFlag = models.TextField()
url = models.TextField()

class Meta:
managed = False
db_table = 'rio1'

class Rio2(models.Model):
datestr = models.DateTimeField()
source = models.TextField()
dFlag = models.TextField()
url = models.TextField()

class Meta:
managed = False
db_table = 'rio2'

In each table there is a primary ID column, but it does not have the same values in both tables, only datestr and source are the identical, now the trick is that both datestr and source are appearing 4 times per every single day, hence I can't set datestr or source to be unique primary keys. 

When I did a simple dummy join, the Django query yields no results because it is joing on the ID column which is of course won't bring back any matched records from the two tables. What would be the correct way to join such tables?

Many thanks!











Melvyn Sopacua

unread,
Mar 8, 2017, 7:02:45 AM3/8/17
to django...@googlegroups.com

On Tuesday 07 March 2017 10:50:24 aysh...@gmail.com wrote:

> Greetings,

>

> I am trying to do a join on the following 2 models (tables)

 

Models cannot be joined. There is no Model.join(). Models are objects you can relate to eachother.

Once you stop thinking in database terms, the problem is easily solved.

 

Since the two models are identical, why would you use two models? Your problem starts with your design - rethink it.

--

Melvyn Sopacua

aysh...@gmail.com

unread,
Mar 8, 2017, 9:23:44 AM3/8/17
to Django users

Thanks for your answer, the models represent two different tables, identical structure but have to be segregated, there is no better design options to implement them. Any leads on how to do the join?

Melvyn Sopacua

unread,
Mar 8, 2017, 10:37:00 AM3/8/17
to django...@googlegroups.com

On Wednesday 08 March 2017 06:23:44 aysh...@gmail.com wrote:

> Thanks for your answer, the models represent two different tables,

> identical structure but have to be segregated, there is no better

> design options to implement them. Any leads on how to do the join?

 

Think about what the join needs to do. Find duplicates? Get all of them in either table?

Provide the right question and Django will do the right join.

--

Melvyn Sopacua

Matthew Pava

unread,
Mar 8, 2017, 11:11:42 AM3/8/17
to django...@googlegroups.com

Why are there no better design options to implement them?  Are the tables being used in another software that you have no control over?

I would just merge the two tables and use one model.

--
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/5f7348a8-2267-4b18-96ee-624b22f63a5f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

aysh...@gmail.com

unread,
Mar 8, 2017, 11:39:43 AM3/8/17
to Django users, Matthe...@iss.com
Tables are coming from different sources, for each single datetime value there is a different url, in order to tie up the single source data it must live inside its own table.


On Wednesday, March 8, 2017 at 9:11:42 AM UTC-7, Matthew Pava wrote:

Why are there no better design options to implement them?  Are the tables being used in another software that you have no control over?

I would just merge the two tables and use one model.

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of aysh...@gmail.com
Sent: Wednesday, March 8, 2017 8:24 AM
To: Django users
Subject: Re: Simple Join with no matching primary keys

 

 

Thanks for your answer, the models represent two different tables, identical structure but have to be segregated, there is no better design options to implement them. Any leads on how to do the join?

 

 

Models cannot be joined. There is no Model.join(). Models are objects you can relate to eachother.

Once you stop thinking in database terms, the problem is easily solved.

 

Since the two models are identical, why would you use two models? Your problem starts with your design - rethink it.

--

Melvyn Sopacua

--
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 djang...@googlegroups.com.

Scot Hacker

unread,
Mar 10, 2017, 2:26:35 AM3/10/17
to Django users, Matthe...@iss.com


On Wednesday, March 8, 2017 at 8:39:43 AM UTC-8, aysh...@gmail.com wrote:
Tables are coming from different sources, for each single datetime value there is a different url, in order to tie up the single source data it must live inside its own table.

So why not make them into one table? You already have a field for `source` to distinguish the data source of each. Two tables with an identical set of fields is a code smell, IMO. 

./s

Camilo Torres

unread,
Mar 11, 2017, 10:56:13 AM3/11/17
to Django users
You can also create a database view from the join of the two tables.
Reply all
Reply to author
Forward
0 new messages