Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “运行后”;makemigrations“;及;迁移;,未在PostgreSQL中创建表_Python_Django_Postgresql - Fatal编程技术网

Python “运行后”;makemigrations“;及;迁移;,未在PostgreSQL中创建表

Python “运行后”;makemigrations“;及;迁移;,未在PostgreSQL中创建表,python,django,postgresql,Python,Django,Postgresql,我正在尝试为我的Django项目中集成的新应用程序向我的数据库中添加额外的表 当我运行python3 manage.py makemigrations和python3 manage.py migrate时,我可以看到模型已经创建,但是相关的表没有添加到我的数据库中。正在添加帖子、偏好和评论表,但未添加技能表、交换表、Tagulous_交换表、标题表和Tagulous_交换表。(我看不出为什么没有添加它们的任何错误) 这是我的迁移文件: # Generated by Django 3.2.2 on

我正在尝试为我的Django项目中集成的新应用程序向我的数据库中添加额外的表

当我运行
python3 manage.py makemigrations
python3 manage.py migrate
时,我可以看到模型已经创建,但是相关的表没有添加到我的数据库中。正在添加帖子、偏好和评论表,但未添加技能表、交换表、Tagulous_交换表、标题表和Tagulous_交换表。(我看不出为什么没有添加它们的任何错误)

这是我的迁移文件:

# Generated by Django 3.2.2 on 2021-05-26 15:22

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import tagulous.models.fields
import tagulous.models.models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

 operations = [
        migrations.CreateModel(
            name='Post',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('content', models.TextField(max_length=1000)),
                ('date_posted', models.DateTimeField(default=django.utils.timezone.now)),
                ('image', models.ImageField(default='default.png', upload_to='srv_media')),
                ('likes', models.IntegerField(default=0)),
                ('dislikes', models.IntegerField(default=0)),
                ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='Skill',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255, unique=True)),
                ('slug', models.SlugField()),
                ('count', models.IntegerField(default=0, help_text='Internal counter of how many times this tag is in use')),
                ('protected', models.BooleanField(default=False, help_text='Will not be deleted when the count reaches 0')),
                ('path', models.TextField()),
                ('label', models.CharField(help_text='The name of the tag, without ancestors', max_length=255)),
                ('level', models.IntegerField(default=1, help_text='The level of the tag in the tree')),
                ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='sfsrv.skill')),
            ],
            options={
                'ordering': ('name',),
                'abstract': False,
                'unique_together': {('slug', 'parent')},
            },
            bases=(tagulous.models.models.BaseTagTreeModel, models.Model),
        ),
        migrations.CreateModel(
            name='Tagulous_SwapTags_title',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255, unique=True)),
                ('slug', models.SlugField()),
                ('count', models.IntegerField(default=0, help_text='Internal counter of how many times this tag is in use')),
                ('protected', models.BooleanField(default=False, help_text='Will not be deleted when the count reaches 0')),
            ],
            options={
                'ordering': ('name',),
                'abstract': False,
                'unique_together': {('slug',)},
            },
            bases=(tagulous.models.models.BaseTagModel, models.Model),
        ),
        migrations.CreateModel(
            name='Tagulous_SwapTags_hobbies',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255, unique=True)),
                ('slug', models.SlugField()),
                ('count', models.IntegerField(default=0, help_text='Internal counter of how many times this tag is in use')),
                ('protected', models.BooleanField(default=False, help_text='Will not be deleted when the count reaches 0')),
            ],
            options={
                'ordering': ('name',),
                'abstract': False,
                'unique_together': {('slug',)},
            },
            bases=(tagulous.models.models.BaseTagModel, models.Model),
        ),
        migrations.CreateModel(
            name='SwapTags',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255)),
                ('hobbies', tagulous.models.fields.TagField(_set_tag_meta=True, blank=True, force_lowercase=True, help_text='This field splits on spaces and commas', initial='eating, coding, gaming', to='sfsrv.Tagulous_SwapTags_hobbies')),
                ('skills', tagulous.models.fields.TagField(_set_tag_meta=True, autocomplete_view='swaptags_skills_autocomplete', help_text='This field does not split on spaces', initial='"Accountancy/Tax Returns", "Decorator/Residential Decorator", "Gardener/Home Garden Maintenance", "Gardener/Public Spaces", "Plumber/Home Plumber", Accountancy/Bookkeeping', space_delimiter=False, to='sfsrv.Skill', tree=True)),
                ('title', tagulous.models.fields.SingleTagField(_set_tag_meta=True, blank=True, help_text='This is a SingleTagField - effectively a CharField with dynamic choices', initial='Accountant, Decorator', null=True, on_delete=django.db.models.deletion.CASCADE, to='sfsrv.tagulous_swaptags_title')),
            ],
            options={
                'verbose_name_plural': 'swaptags',
            },
        ),
        migrations.CreateModel(
            name='Comment',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('content', models.TextField(max_length=150)),
                ('date_posted', models.DateTimeField(default=django.utils.timezone.now)),
                ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
                ('post_connected', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sfsrv.post')),
            ],
        ),
        migrations.CreateModel(
            name='Preference',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('value', models.IntegerField()),
                ('date', models.DateTimeField(auto_now=True)),
                ('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sfsrv.post')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'unique_together': {('user', 'post', 'value')},
            },
        ),
    ]
管理员

from django.contrib import admin
from django.db import models
import tagulous.admin
from sfsrv.models import Post, Comment, Preference, SwapTags, Skill

admin.site.register(Post)
admin.site.register(Comment)
admin.site.register(Preference)

# Register the SwapTags with a custom ModelAdmin
class SwapTagsAdmin(admin.ModelAdmin):
    list_display = ("name", "title", "skills", "hobbies")
    list_filter = ("name", "title", "skills", "hobbies")

tagulous.admin.register(SwapTags, SwapTagsAdmin)

# Auto-gen the ModelAdmins for skills and hobbies
tagulous.admin.register(Skill)
tagulous.admin.register(SwapTags.hobbies.tag_model)


# Give the Title ModelAdmin the people as inlines
class SwapTagsInline(admin.TabularInline):
    model = SwapTags
    extra = 3


class TitleAdmin(admin.ModelAdmin):
    inlines = [SwapTagsInline]


tagulous.admin.register(SwapTags.title.tag_model, TitleAdmin)
型号.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from PIL import Image
import tagulous.models



class Post(models.Model):
    content = models.TextField(max_length=1000)
    date_posted = models.DateTimeField(default=timezone.now)
    image = models.ImageField(default='default.png', upload_to='srv_media')
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    likes= models.IntegerField(default=0)
    dislikes= models.IntegerField(default=0)

    def __str__(self):
        return self.content[:5]

        img = Image.open(self.image.path)
        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

    @property
    def number_of_comments(self):
        return Comment.objects.filter(post_connected=self).count()

class Comment(models.Model):
    content = models.TextField(max_length=150)
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post_connected = models.ForeignKey(Post, on_delete=models.CASCADE)


class Preference(models.Model):
    user= models.ForeignKey(User, on_delete=models.CASCADE)
    post= models.ForeignKey(Post, on_delete=models.CASCADE)
    value= models.IntegerField()
    date= models.DateTimeField(auto_now= True)

    def __str__(self):
        return str(self.user) + ':' + str(self.post) +':' + str(self.value)

    class Meta:
       unique_together = ("user", "post", "value")
       
class Skill(tagulous.models.TagTreeModel):
    class TagMeta:
        initial = [
            "Accountancy/Bookkeeping",
            "Accountancy/Tax Returns",
            "Decorator/Residential Decorator",
            "Plumber/Home Plumber",
            "Gardener/Home Garden Maintenance",
            "Gardener/Public Spaces",
        ]
        space_delimiter = False
        autocomplete_view = "swaptags_skills_autocomplete"


class SwapTags(models.Model):
    name = models.CharField(max_length=255)
    title = tagulous.models.SingleTagField(
        initial="Accountant, Decorator",
        help_text=(
            "This is a SingleTagField - effectively a CharField with " "dynamic choices"
        ),
        on_delete=models.CASCADE,
    )
    skills = tagulous.models.TagField(
        Skill, help_text="This field does not split on spaces"
    )
    hobbies = tagulous.models.TagField(
        initial="eating, coding, gaming",
        force_lowercase=True,
        blank=True,
        help_text="This field splits on spaces and commas",
    )

    class Meta:
        verbose_name_plural = "swaptags"

欢迎提出任何建议!谢谢:-)

删除所有以前的迁移,然后使用这些命令重试。谢谢您的评论。我是否应该使用
python manage.py migrate--fake myapp zero
清除迁移历史记录?然后尝试重新运行迁移(删除相关迁移文件后?)是的,您尝试过了吗?@Jarjar95
python manage.py migrate--fake myapp zero
不适合您的情况,它只会让您更加困难。“相关表没有被添加到我的数据库中”您是如何检查的?您是否在设置中定义了
数据库路由器
?@Jarjar95我假设您在运行时使用了
--fake
标志,但迁移不正确。我假设您得到了错误,然后使用了
--false
,然后删除了迁移(通常删除迁移可能会出错,除非您知道自己正在做什么,或者实际上也要删除数据库),您现在正处于这种情况是吗?如果没有什么重要的事情,我会建议干脆删除数据库。否则,您将无法确定数据库的正确状态,并手动分离/编辑迁移文件作为修复。