我已将一些指标事件导入Django项目。接下来,我将事件分组为会话,定义为给定时间窗口内的一系列连续事件。
我的models.py看起来像这样:
from django.db import models
from django.contrib.auth.models import User
class Event(models.Model):
name = models.CharField(max_length=255, blank=True, db_index=True)
id1 = models.CharField(max_length=255, blank=True, db_index=True)
id2 = models.CharField(max_length=255, blank=True)
time = models.DateTimeField(db_index=True)
user = models.ForeignKey(User, related_name='events', null=True, db_index=True)
session = models.ForeignKey('Session', related_name='events', null=True, db_index=True)
def __unicode__(self):
return '"%s" event' % self.name or 'unnamed'
class Session(models.Model):
user = models.ForeignKey(User, related_name='sessions', null=True)
我现在正试图对这些会话的开始和结束时间进行非规范化,以避免在所有会话事件上进行MAX和MIN聚合。所以我的新Session模型看起来像这样。
class Session(models.Model):
user = models.ForeignKey(User, related_name='sessions', null=True)
start = models.DateTimeField(db_index=True, null=True)
end = models.DateTimeField(db_index=True, null=True)
我添加了字段,创建了架构迁移(运行正常)并创建了数据迁移:
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(DataMigration):
def forwards(self, orm):
"Write your forwards methods here."
orm.Session.objects \
.annotate(
start_time=models.Min('events__time'),
end_time=models.Max('events__time')) \
.update(start=models.F('start_time'), end=models.F('end_time'))
def backwards(self, orm):
"Write your backwards methods here."
pass
models = {
'auth.group': {
...
'metrics_import.event': {
'Meta': {'object_name': 'Event'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'id1': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}),
'id2': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}),
'session': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'events'", 'null': 'True', 'to': "orm['metrics_import.Session']"}),
'time': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'events'", 'null': 'True', 'to': "orm['auth.User']"})
},
'metrics_import.session': {
'Meta': {'object_name': 'Session'},
'end': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'start': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'db_index': 'True'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'sessions'", 'null': 'True', 'to': "orm['auth.User']"})
}
}
看起来很简单,但运行数据迁移会产生:
django.db.utils.DatabaseError: no such column: metrics_import_event.time
我已经检查过该列确实存在,都来自django shell:
>>> Event.objects.all()[0].time
datetime.datetime(2012, 1, 19, 3, 0, 3)
直接在数据库架构中:
sqlite> .schema metrics_import_event
CREATE TABLE "metrics_import_event" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(255) NOT NULL,
"id1" varchar(255) NOT NULL,
"id2" varchar(255) NOT NULL,
"time" datetime NOT NULL,
"user_id" integer REFERENCES "auth_user" ("id"),
"session_id" integer
);
CREATE INDEX "metrics_import_event_3a04cc98" ON "metrics_import_event" ("time");
CREATE INDEX "metrics_import_event_52094d6e" ON "metrics_import_event" ("name");
CREATE INDEX "metrics_import_event_6b4dc4c3" ON "metrics_import_event" ("session_id");
CREATE INDEX "metrics_import_event_fbfc09f1" ON "metrics_import_event" ("user_id");
我确信我有些愚蠢的东西,但我无法弄清楚是什么。
谢谢!
编辑:请注意,初始架构迁移已应用--fake
,以防以某种方式重要。
答案 0 :(得分:0)
请注意,您正在尝试使用连接运行更新语句 - 我很确定Sqlite不支持这种情况;也就是说,它可能是使用'with'子句实现的,但是我怀疑Django是这样做的(因为with-clause只在版本3.8.3中添加到SQLite,在Django 1.6之后发布很久)。我想你得到的东西非常接近:
$ sqlite3
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table x (a int, b int);
sqlite> insert into x values(1,2);
sqlite> select * from x;
1|2
sqlite> create table y (c int, d int);
sqlite> update x set b = min (y.d);
Error: no such column: y.d
我认为您可以设置日志记录以在执行之前打印所有SQL,这样您就可以获得更多信息。
(这个答案猜测,实际上,这与南方无关 - 问题出在Django上)。