I defined two models:
class Server(models.Model):
owners = models.ManyToManyField('Person')
class Person(models.Model):
name = models.CharField(max_length=50)
admin.site.register(Server)
admin.site.register(Person)
After that I even checked the sql, just for fun:
BEGIN;
CREATE TABLE "servers_server_owners" (
"id" integer NOT NULL PRIMARY KEY,
"server_id" integer NOT NULL,
"person_id" integer NOT NULL,
UNIQUE ("server_id", "person_id")
)
;
CREATE TABLE "servers_server" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(50) NOT NULL,
"port" integer unsigned NOT NULL,
"state" integer NOT NULL
)
;
CREATE TABLE "servers_person" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(50) NOT NULL
)
;
COMMIT;
There it even says CREATE TABLE "servers_server_owners"
I ran syncdb
to install the new models to the database. I went to the admin-interface to define some objects to play with, but I got the following error:
DatabaseError at /admin/servers/server/1/
no such table: servers_server_owners
I shutdown the dev-server, ran syncdb
again, started the server: Still same problem. Why can't it find, the table, even though it just told me it created id?
syncdb is a command which is executed in django shell to create tables for first time for apps which are added to INSTALLED_APPS of settings.py. Need to keep in mind about two key words: 'First Time' and 'Newly Added Apps'.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With