database design problem.

40 views
Skip to first unread message

willy Hakizimana

unread,
Mar 15, 2014, 11:38:03 AM3/15/14
to django...@googlegroups.com
First of all, this community is amazing at how fast questions are answered. I have learned so much.


I am designing an app with models that look like this.

Country(country_id(PK), country_name, gdp, gdp_growth, income_per_capita)

Product(product_id(PK), product_name, country_id(FK), imports_volume)

Since I have to show imports_volume for the same product in each country, I am stumped on how to design the db.


 

Larry Martell

unread,
Mar 15, 2014, 12:33:40 PM3/15/14
to django...@googlegroups.com
I'd have a country table, a product table, and a product_country table:

Country(country_id(PK), country_name, gdp, gdp_growth, income_per_capita)
Product(product_id(PK), product_name)
Product_Country(id(PK), product_id(FK), country_id(FK), imports_volume)
Message has been deleted

Alvin Lindstam

unread,
Mar 15, 2014, 4:25:12 PM3/15/14
to django...@googlegroups.com, wlf...@ix.netcom.com
Actually, the Django term for this is "through". Not via.

See https://docs.djangoproject.com/en/1.6/topics/db/models/#extra-fields-on-many-to-many-relationships on how to specify the intermediate table to be used in many-to-many relationships.


Den lördagen den 15:e mars 2014 kl. 18:01:02 UTC+1 skrev Dennis Lee Bieber:
On Sat, 15 Mar 2014 08:38:03 -0700 (PDT), willy Hakizimana
<will...@gmail.com> declaimed the following:
        If I understand your statement, you need (paraphrased) the
imports_volume to be for each product for each country...

        That means it does NOT belong in either Country or Product tables.

Country(*countryID*, Name, gdp, gdp_growth, per_capita_income)

Product(*productID*,  Name)

Imports(_countryID_, _productID_, import_volume)

        In Django terms (I believe), this is a many2many relationship using a
via table (the native m2m would create a table with _countryID_,
_productID_ foreign keys only; the via table lets you add data specific to
the relationship itself... An intersect table in older database
terminology)

>
>
--
        Wulfraed                 Dennis Lee Bieber         AF6VN
    wlf...@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

willy Hakizimana

unread,
Mar 15, 2014, 6:41:23 PM3/15/14
to django...@googlegroups.com
Thank you guys so much. You guys rock!

Lachlan Musicman

unread,
Mar 16, 2014, 12:41:18 AM3/16/14
to django...@googlegroups.com
I would make the imports model an "intermediate model" - see here:

https://docs.djangoproject.com/en/1.6/topics/db/models/#extra-fields-on-many-to-many-relationships

L.
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/03becba4-4127-4bac-b76d-c47e2cbd7b74%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
From this perspective it is natural that anarchism be marked by
spontaneity, differentiation, and experimentation that it be marked by
an expressed affinity with chaos, if chaos is understood to be what
lies outside or beyond the dominant game or system. Because of the
resistance to definition and categorisation, the anarchist principle
has been variously interpreted as, rather than an articulated
position, “a moral attitude, an emotional climate, or even a mood”.
This mood hangs in dramatic tension between utopian hope or dystopian
nihilism...
-----
http://zuihitsu.org/godspeed-you-black-emperor-and-the-politics-of-chaos
Reply all
Reply to author
Forward
0 new messages