我已经用Django建立了一个项目,并在模型中定义了一些表格,还有表格“Artikel”:
class Artikel(models.Model):
anr = models.IntegerField(primary_key=True)
anzahl = models.CharField(max_length=5)
bez = models.CharField(max_length=255)
preis = models.DecimalField(max_digits=7, decimal_places=2)
info = models.CharField(max_length=255)
def __str__(self):
return 'Anr: {}, Anzahl: {}'.format(self.anr, self.anzahl)
然后我使用makemigration和migrate将已定义的表迁移到数据库(runserver也可以在没有错误的情况下工作)但是当我现在用命令打开shell时:
python manage.py shell
当我使用以下命令创建对象并保存时:
from shop.models import *
a = Artikel(anr=1, anzahl=1, bez='test', preis=1.99, info='test')
a.save()
我通过方法a.save()
得到以下错误Traceback (most recent call last):
File "<console>", line 1, in <module>
File "E:\Python34\lib\site-packages\django\db\models\base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "E:\Python34\lib\site-packages\django\db\models\base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "E:\Python34\lib\site-packages\django\db\models\base.py", line 889, in _save_table
forced_update)
File "E:\Python34\lib\site-packages\django\db\models\base.py", line 939, in _do_update
return filtered._update(values) > 0
File "E:\Python34\lib\site-packages\django\db\models\query.py", line 654, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "E:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 1148, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "E:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "E:\Python34\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "E:\Python34\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "E:\Python34\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "E:\Python34\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "E:\Python34\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "E:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: shop_artikel
生成的迁移数据:
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Adresse',
fields=[
('strasse', models.CharField(max_length=255, primary_key=True)),
('hnr', models.IntegerField(primary_key=True)),
('plz', models.CharField(max_length=20, primary_key=True, serialize=False)),
('ort', models.CharField(max_length=255, primary_key=True)),
],
options={
'verbose_name_plural': 'Adressen',
},
),
migrations.CreateModel(
name='Artikel',
fields=[
('anr', models.IntegerField(primary_key=True, serialize=False)),
('anzahl', models.CharField(max_length=5)),
('bez', models.CharField(max_length=255)),
('preis', models.DecimalField(decimal_places=2, max_digits=7)),
('info', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name='Bestellung',
fields=[
('bestellnummer', models.IntegerField(primary_key=True, serialize=False)),
('datum', models.DateField()),
('status', models.CharField(max_length=20)),
],
),
migrations.CreateModel(
name='Feedback',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bemerkung', models.CharField(max_length=255)),
('datum', models.DateField()),
],
),
migrations.CreateModel(
name='Kunde',
fields=[
('knr', models.IntegerField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=255)),
('email', models.CharField(max_length=255)),
('zuletzt_online', models.DateField(null=True)),
('adresse', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Adresse')),
],
),
migrations.CreateModel(
name='Land',
fields=[
('name', models.CharField(max_length=255, primary_key=True, serialize=False)),
],
),
migrations.CreateModel(
name='Warenkorb',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('anzahl', models.CharField(max_length=3)),
],
),
migrations.CreateModel(
name='Bluray',
fields=[
('artikel_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='shop.Artikel')),
('regisseur', models.CharField(max_length=50)),
('jahr', models.DateField()),
('genre', models.CharField(max_length=50)),
],
bases=('shop.artikel',),
),
migrations.CreateModel(
name='Buch',
fields=[
('artikel_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='shop.Artikel')),
('autor', models.CharField(max_length=50)),
('verlag', models.CharField(max_length=50)),
('isbn', models.CharField(max_length=13)),
],
bases=('shop.artikel',),
),
migrations.CreateModel(
name='SonstigerArtikel',
fields=[
('artikel_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='shop.Artikel')),
],
bases=('shop.artikel',),
),
migrations.AddField(
model_name='warenkorb',
name='artikel',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Artikel'),
),
migrations.AddField(
model_name='warenkorb',
name='bestellung',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Bestellung'),
),
migrations.AddField(
model_name='feedback',
name='artikel',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Artikel'),
),
migrations.AddField(
model_name='feedback',
name='kunde',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Kunde'),
),
migrations.AddField(
model_name='bestellung',
name='artikel',
field=models.ManyToManyField(through='shop.Warenkorb', to='shop.Artikel'),
),
migrations.AddField(
model_name='bestellung',
name='kunde',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Kunde'),
),
migrations.AddField(
model_name='bestellung',
name='lieferadr',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.Adresse'),
),
migrations.AddField(
model_name='adresse',
name='land',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='shop.Land'),
),
]
答案 0 :(得分:3)
删除迁移文件并删除数据库并重新创建。然后,
rm -r shop/migrations
rm -rf db.sqlite3
python manage.py makemigrations
python manage.py migrate
检查您是否已添加&#34; shop&#34;在您的settings.py中的INSTALLED_APPS
然后打开shell,再试一次。