what wrong with my models.py

走远了吗. 提交于 2021-01-28 18:59:21

问题


I am using Django 1.4 with PostgreSQL 9.1.4

what wrong with my models.py

class UserProfile(models.Model):
    ROLES_TYPE = (
        ('Employee', 'Employee'),
        ('Employor', 'Employor'),
    )
    user = models.OneToOneField(User, related_name='Profile')
    role = models.CharField(max_length=15, choices=ROLES_TYPE)

class Student(models.Model):
    id = models.AutoField(db_column='studentid', primary_key=True)
    username = models.OneToOneField(UserProfile, related_name='Employee')
    first_name = models.CharField(db_column='fname', max_length=100,
                              null=True, blank=True)
    last_name = models.CharField(db_column='lname', max_length=100,
                             null=True, blank=True)

When I run ./manage.py syncdb , i got error

Creating tables ...
Creating table Student
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
  File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 102, in handle_noargs
    cursor.execute(statement)
  File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
  File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
    return self.cursor.execute(query, args)
django.db.utils.DatabaseError: column "id" referenced in foreign key constraint does not exist

SQL Query after removing related names and ID field

CREATE TABLE "admin_userprofile" (

 "id" serial NOT NULL PRIMARY KEY,
       "user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id") DEFERRABLE     INITIALLY DEFERRED,
        "role" varchar(15) NOT NULL
    );

CREATE TABLE "Student" (

       "id" serial NOT NULL PRIMARY KEY,
       "username_id" integer NOT NULL UNIQUE REFERENCES "admin_userprofile" ("id") DEFERRABLE INITIALLY DEFERRED
    );

Please help me.

Thanks & Regards, Jayapal D


回答1:


This is a problem generated by your database not django itself. Try to recreate your database schema (you can just remove all related columns and execute ./manage.py syncdb.




回答2:


Try to remove db_column='studentid' from your id column in Student model.

Why are you changing column name?

Also, why are you not using auto-id for it?



来源:https://stackoverflow.com/questions/11898908/what-wrong-with-my-models-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!