Django - DatabaseError: No such table

前端 未结 5 828
不知归路
不知归路 2021-02-04 11:12

I defined two models:

class Server(models.Model):
    owners = models.ManyToManyField(\'Person\')

class Person(models.Model):
    name = models.CharField(max_le         


        
相关标签:
5条回答
  • 2021-02-04 11:39

    Actually the problem was that the table never got created. Since I am fairly new with django, I did not know that ./manage.py syncdb does not update existing models, but only creates the ones that do not exist.

    Because the model 'Server' existed before I added the other model, and it was already in the db, 'syncdb' did not actually create the new tables.

    0 讨论(0)
  • 2021-02-04 11:41

    for django 1.9, this is what i did and it solved the issue.

    python manage.py makemigrations app_name

    python manage.py migrate

    0 讨论(0)
  • 2021-02-04 11:44
    1. use "manage.py help" to check the command
    2. if you find migrate and makemigration, it means your python has updated

    step 1:

    python manage.py makemigration
    

    result 1:

    Migrations for 'mainsite':
    mainsite\migrations\0001_initial.py
    - Create model Post
    

    step 2:

    python manage.py migrate
    

    result 2:

    Operations to perform:
    Apply all migrations: admin, auth, contenttypes, mainsite, sessions
    Running migrations:
    Applying mainsite.0001_initial... OK
    

    finally, runserver. Done

    0 讨论(0)
  • 2021-02-04 11:47

    I meet the same problem today and fix it. I think you miss some command in tutorial 1. just do follow:

    ./python manage.py makemigrations polls
    python manage.py sql polls
    ./python manage.py syncdb
    

    then fix it and gain the table polls and you can see the table created. you should read the manage.py makemigrations command.

    0 讨论(0)
  • 2021-02-04 12:02

    As a tip for the future, look into South, a very useful utility for applying your model changes to the database without having to create a new database each time you've changed the model(s).

    With it you can easily: python manage.py migrate app_name and South will write your model changes. The documentation is pretty straightforward.

    0 讨论(0)
提交回复
热议问题