migrating from sqlite to mysql breaks the site

51 views
Skip to first unread message

Pedram Badakhchani

unread,
Mar 6, 2019, 5:41:17 AM3/6/19
to Django users
Hi All,

I am currently going through Django 2 by Example Book  by Antonio Melé.

Unfortunately I have a problem with the source code provided on the associated git hub pages:

specifically in chapter 1 :


After downloading the source files, and setting up the chapter 1 example locally, using the default sqlite database  everything works as expected.

However, changing the database to mysql the site breaks. This is the scenario:

DATABASES = {
   
'default': {
       
'ENGINE': 'django.db.backends.mysql',
       
'NAME': 'pedbad',
       
'USER': 'root',
       
'PASSWORD': 'root',
       
'HOST': 'localhost',
       
'PORT': '3306',
   
}
}


Changing the settings.py file to use mysql works fine, and the database is created, and the tables are all populated correctly. using createsuperuser also works and I am able to access the admin backend and create a post...
navigating to :  http://127.0.0.1:8000/blog/ shows the post and the post title link is set to: http://127.0.0.1:8000/blog/2019/1/31/test-post/ however, although this works fine with sqllite, on mysql the link take me to a 404 page: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/blog/2019/1/31/test-post/ Raised by: blog.views.post_detail No Post matches the given query. even though the blog post exists in the mysql table: table blog_post id - 1 title - Test Post slug - test-post body - content for test post publish - 2019-01-31 10:56:40.000000 created - 2019-01-31 10:56:59.674959 updated - 2019-01-31 10:56:59.674976 status - published author_id -  1 I would appreciate any advice on why this is happening when the database is switched to MySQl

I suspect the problem is in the blog model:



from django.db import models
from django.utils import timezone
 
from django.contrib.auth.models import User
 
from django.urls import reverse
 
 
 
class PublishedManager(models.Manager):
 
def get_queryset(self):
 
return super(PublishedManager, self).get_queryset().filter(status='published')
 
 
 
class Post(models.Model):
 STATUS_CHOICES
= (
 
('draft', 'Draft'),
 
('published', 'Published'),
 
)
 title
= models.CharField(max_length=250)
 slug
= models.SlugField(max_length=250,
 unique_for_date
='publish')
 author
= models.ForeignKey(User,
 on_delete
=models.CASCADE,
 related_name
='blog_posts')
 body
= models.TextField()
 publish
= models.DateTimeField(default=timezone.now)
 created
= models.DateTimeField(auto_now_add=True)
 updated
= models.DateTimeField(auto_now=True)
 status
= models.CharField(max_length=10,
 choices
=STATUS_CHOICES,
 
default='draft')
 
 objects
= models.Manager() # The default manager.
 published
= PublishedManager() # Our custom manager.
 
 
class Meta:
 ordering
= ('-publish',)
 
 
def __str__(self):
 
return self.title
 
 
def get_absolute_url(self):
 
return reverse('blog:post_detail',
 args
=[self.publish.year,
 
self.publish.month,
 
self.publish.day,
 
self.slug])

thank you Pedram

Pedram Badakhchani

unread,
Mar 6, 2019, 5:44:56 AM3/6/19
to Django users
specifically in the get_absolute_url method.

I don't understand why this works fine with sqlite but breaks in mysql.

thanks for any help. 

Pedram Badakhchani

unread,
Mar 13, 2019, 11:38:07 AM3/13/19
to Django users
bump

Anh Nguyen

unread,
Mar 13, 2019, 11:47:54 AM3/13/19
to django...@googlegroups.com
Could you insert a post on admin panel ?

--
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/4db98bc9-72f6-4914-8d48-7ae60308a14e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pedram Badakhchani

unread,
Mar 14, 2019, 6:06:45 AM3/14/19
to Django users
Hi Anh,

yes, everything works fine, the only problem is once I switched from sqlite to mysql, 

I am able to access the admin backend and create a post...

I am able to view the list of posts created, however when I click a given post, with SQLite I can navigate to
a page showing the post details as expected.

However, switching to mysql breaks the redirecting to post details. I get No Post matches the given query.

I am not sure why this happens. I thought the Django ORM means that the code should work regardless of the
database used. So I am confused why this happens.

best

Pedram

Ayser shuhaib

unread,
Mar 14, 2019, 6:26:19 AM3/14/19
to django...@googlegroups.com
In your settings, you put the port is 3306 but in the url it’s 8000.
I think the problem is there.

Anh Nguyen

unread,
Mar 14, 2019, 8:05:28 AM3/14/19
to django...@googlegroups.com
I see 2 ways to handle.
1: would you want to try postgresql ?, if this get the same trouble. We got the big problems.
2: try to create a blank project start from django-admin ... it’s not greater then 20mins. 



Reply all
Reply to author
Forward
0 new messages