models foreign key

47 views
Skip to first unread message

jeffreyequizuvero

unread,
Jan 12, 2017, 9:40:43 AM1/12/17
to Django users

This is about the models foreign key,
I have two tables and i used foreign key to connect these tables however after i did migration it's not working. Is there any wrong with my code?


please help, Thanks.



class Author(models.Model):
    Author_code = models.CharField(max_length=50,unique=True)
    Author_Fname = models.CharField(max_length=30, blank=True, null=True)
    Author_Mname = models.CharField(max_length=30, blank=True, null=True)
    Author_Lname = models.CharField(max_length=30, blank=True, null=True)

    class Meta:
#         managed = False
        db_table = 'Author'


class Book(models.Model):

    Book_code = models.CharField(max_length=50, blank=True, default=user_code_key,unique=True)
    Author_code = models.ForeignKey(Author, to_field=Author_code', on_delete=models.CASCADE)
    Book_title = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
 #       managed = False
        db_table = 'Book'




jorr...@gmail.com

unread,
Jan 12, 2017, 12:18:01 PM1/12/17
to Django users
What exactly isn't working? Are you getting an error message?

I can see a few potential problems:
  • You're missing a quotation mark around Author_code in the to_field parameter;
  • The fields on both models have the same name, this might cause problems.

Jeffrey Uvero

unread,
Jan 12, 2017, 11:43:03 PM1/12/17
to django...@googlegroups.com
there's no problem appeared but when I go to my database, there's no connection with these two table.. there should be a connection between two same name column.(foreign key)

Correction with my code above.
Author_code = models.ForeignKey(Author, to_field='Author_code', on_delete=models.CASCADE)
there's a quotation mark in this column, My fault I forgot to put it above.

--
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...@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/fb32e401-f1a6-4180-a49a-12c86b09bed5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Jeffrey E. Uvero

Amellar Solution

3rd Floor, LGI Building, Ortigas Avenue

Greenhills, San Juan City, 

Philippines 1500

Tel: (+63) 9776824461

SkypeID: jeffrey.uvero

Email: jeffreyeq...@gmail.com / jeffe...@gmail.com


Melvyn Sopacua

unread,
Jan 14, 2017, 4:17:09 PM1/14/17
to django...@googlegroups.com

On Friday 13 January 2017 07:42:17 Jeffrey Uvero wrote:

> *there's no problem appeared but when I go to my database, there's no

> connection with these two table.. there should be a connection between

> two same name column.(foreign key)*

> Correction with my code above.

 

How did you determine there's no connection? What database are using? If using MySQL do you have InnoDB storage engine enabled?

--

Melvyn Sopacua

Jeffrey Uvero

unread,
Jan 15, 2017, 11:56:11 PM1/15/17
to django...@googlegroups.com
I am using oracle.
actually I used the procedure before and it was successful, but now i used same procedure but it didn't work.

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Melvyn Sopacua

unread,
Jan 16, 2017, 1:09:18 AM1/16/17
to django...@googlegroups.com

On Monday 16 January 2017 07:55:28 Jeffrey Uvero wrote:

> *I am using oracle. *

>

> *actually I used the procedure before and it was successful, but now i

> used same procedure but it didn't work. *

 

Make sure that the tool you use to "see the connection between these tables" is not the problem.

Use:

python manage.py sqlmigrate <app_label> <migration_name>

 

to show the SQL created for that migration. If you see no foreign key being created in the SQL, then follow-up with the code of the migration file and the SQL shown by above command.

 

> On Sun, Jan 15, 2017 at 12:16 AM, Melvyn Sopacua

> <m.r.s...@gmail.com>

> wrote:

> > On Friday 13 January 2017 07:42:17 Jeffrey Uvero wrote:

> > > *there's no problem appeared but when I go to my database, there's

> > > no

> > >

> > > connection with these two table.. there should be a connection

> > > between

> > >

> > > two same name column.(foreign key)*

> > >

> > > Correction with my code above.

> >

> > How did you determine there's no connection? What database are

> > using? If using MySQL do you have InnoDB storage engine enabled?

> >

> > --

> >

> > 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 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/3929969.KToH5YS5at%40devstation

> > <https://groups.google.com/d/msgid/django-users/3929969.KToH5YS5at%4

> > 0devstation?utm_medium=email&utm_source=footer> .

> >

> > For more options, visit https://groups.google.com/d/optout.

 

--

Melvyn Sopacua

Michal Petrucha

unread,
Jan 16, 2017, 6:42:05 AM1/16/17
to Django users
On Thu, Jan 12, 2017 at 01:40:43AM -0800, jeffreyequizuvero wrote:
>
>
> *This is about the models foreign key, I have two tables and i used foreign
> key to connect these tables however after i did migration it's not working.
> Is there any wrong with my code? *

I can see that you have a commented-out “managed = False” in the
definition of your models. Did you create and apply migrations before,
or after you commented the attribute out? If you ran your migrations
with managed = False, then it's expected that those tables are ignored
for migrations.

If that's not it, then I'm afraid you'll have to provide more details
than “it's not working” – what steps did you take exactly, what was
the output, what output did you expect, etc.

Good luck,

Michal

> *please help, Thanks. *
>
>
> class *Author*(models.Model):
> * Author_code = models.CharField(max_length=50,unique=True)*
> Author_Fname = models.CharField(max_length=30, blank=True, null=True)
> Author_Mname = models.CharField(max_length=30, blank=True, null=True)
> Author_Lname = models.CharField(max_length=30, blank=True, null=True)
>
> class Meta:
> # managed = False
> db_table = 'Author'
>
>
> class *Book*(models.Model):
>
> Book_code = models.CharField(max_length=50, blank=True,
> default=user_code_key,unique=True)
> * Author_code = models.ForeignKey(Author, to_field=Author_code',
> on_delete=models.CASCADE)*
> Book_title = models.CharField(max_length=50, blank=True, null=True)
>
> class Meta:
> # managed = False
> db_table = 'Book'
>
>
>
>
> --
> 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/f8a12da9-8983-4375-b7ff-fa631d4522a0%40googlegroups.com.
signature.asc
Reply all
Reply to author
Forward
0 new messages