Django迁移:不会生成所有列作为模型

问题描述 投票:1回答:1

对于所有表,生成了一些列,而一些列则没有生成。

这是我的模特

personal info.朋友

from django.db import models

# Create your models here.

class Students(models.Model):
    student_name = models.CharField(max_length=100)
    student_id = models.CharField(max_length=50)
    student_dept = models.CharField(max_length=30)
    student_sec = models.CharField(max_length=2)

class StudentDetails(models.Model):
    students = models.ForeignKey(Students, on_delete=models.CASCADE)
    present_address = models.TextField
    permanent_address = models.TextField
    student_phone = models.BigIntegerField
    gpo_box_number = models.IntegerField

class Teachers(models.Model):
    teacher_name = models.CharField(max_length=100)
    teacher_id = models.CharField(max_length=50)
    teacher_dept = models.CharField(max_length=30)

class TeacherDetails(models.Model):
    teachers = models.ForeignKey(Teachers, on_delete=models.CASCADE)
    present_address = models.TextField
    permanent_address = models.TextField
    teacher_phone = models.BigIntegerField
    gpo_box_number = models.IntegerField

student advising.朋友

from django.db import models
from personalInfo.models import Students, Teachers

# Create your models here.

class Advising(models.Model):
    student = models.ForeignKey(Students, on_delete=models.CASCADE)
    teacher = models.ForeignKey(Teachers, on_delete=models.CASCADE)
    subject_matter = models.CharField(max_length=400)
    comment = models.TextField
    starting_time = models.TimeField
    ending_time = models.TimeField
    date = models.DateField

personalInfo.py的迁移详细信息

# Generated by Django 2.0 on 2018-01-03 17:43

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='StudentDetails',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
        migrations.CreateModel(
            name='Students',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('student_name', models.CharField(max_length=100)),
                ('student_id', models.CharField(max_length=50)),
                ('student_dept', models.CharField(max_length=30)),
                ('student_sec', models.CharField(max_length=2)),
            ],
        ),
         migrations.CreateModel(
            name='TeacherDetails',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
        migrations.CreateModel(
            name='Teachers',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('teacher_name', models.CharField(max_length=100)),
                ('teacher_id', models.CharField(max_length=50)),
                ('teacher_dept', models.CharField(max_length=30)),
             ],
        ),
        migrations.AddField(
            model_name='teacherdetails',
            name='teachers',

field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='personalInfo.Teachers'),
        ),
        migrations.AddField(
            model_name='studentdetails',
            name='students',

field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='personalInfo.Students'),
        ),
    ]

studentAdvising的迁移详细信息

# Generated by Django 2.0 on 2018-01-03 17:43

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('personalInfo', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Advising',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('subject_matter', models.CharField(max_length=400)),
                ('student', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='personalInfo.Students')),
                ('teacher', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='personalInfo.Teachers')),
            ],
        ),
    ]

数据库中的表,

mysql> show tables;
+-----------------------------+
| Tables_in_studentAdvising   |
+-----------------------------+
| auth_group                  |
| auth_group_permissions      |
| auth_permission             |
| auth_user                   |
| auth_user_groups            |
| auth_user_user_permissions  |
| django_admin_log            |
| django_content_type         |
| django_migrations           |
| django_session              |
| personalInfo_studentdetails |
| personalInfo_students       |
| personalInfo_teacherdetails |
| personalInfo_teachers       |
| studentAdvisingApp_advising |
+-----------------------------+
15 rows in set (0.00 sec)


mysql> SHOW COLUMNS FROM personalInfo_students;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| student_name | varchar(100) | NO   |     | NULL    |                |
| student_id   | varchar(50)  | NO   |     | NULL    |                |
| student_dept | varchar(30)  | NO   |     | NULL    |                |
| student_sec  | varchar(2)   | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


mysql> SHOW COLUMNS FROM personalInfo_studentdetails;
+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| students_id | int(11) | NO   | MUL | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


mysql> SHOW COLUMNS FROM personalInfo_teachers;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| teacher_name | varchar(100) | NO   |     | NULL    |                |
| teacher_id   | varchar(50)  | NO   |     | NULL    |                |
| teacher_dept | varchar(30)  | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


mysql> SHOW COLUMNS FROM personalInfo_teacherdetails;
+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| teachers_id | int(11) | NO   | MUL | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM studentAdvisingApp_advising;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| subject_matter | varchar(400) | NO   |     | NULL    |                |
| student_id     | int(11)      | NO   | MUL | NULL    |                |
| teacher_id     | int(11)      | NO   | MUL | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

我无法理解问题出在哪里。谷歌上没有关于它的好消息。我该怎么办??

python mysql django
1个回答
0
投票

在您的类定义中,您可以编写如下内容:

class TeacherDetails(models.Model):
    teachers = models.ForeignKey(Teachers, on_delete=models.CASCADE)
    present_address = models.TextField  # call missing
    permanent_address = models.TextField  # call missing
    teacher_phone = models.BigIntegerField  # call missing
    gpo_box_number = models.IntegerField  # call missing

因此缺少对构造函数的调用。您没有将present_addressTextField实例关联,而是将其与TextField类型相关联。

你需要调用构造函数,所以:

class TeacherDetails(models.Model):
    teachers = models.ForeignKey(Teachers, on_delete=models.CASCADE)
    present_address = models.TextField()  # call to constructor
    permanent_address = models.TextField()  # call to constructor
    teacher_phone = models.BigIntegerField()  # call to constructor
    gpo_box_number = models.IntegerField()  # call to constructor

Django将寻找与Model子类关联的属性,这些子类是Field的实例(不是对Field的(子)类的引用)。然后将它们映射到数据库中的列。

© www.soinside.com 2019 - 2024. All rights reserved.