我有一个Twitter API项目,可以在我的localhost(SQLite3)和现有服务器(Postgres)上正常工作。我将它迁移到DigitalOcean(也是Postgres)并且现在遇到了这个错误:value too long for type character varying(25)
我理解错误意味着什么,但感觉好像我采取的步骤应该改变它。
我不想截断数据(as suggested here),因此希望能够读取任何引发错误的内容,并且此相同代码适用于另一台服务器(Centos6),但不适用于DO( Debian的)。两者都在运行PostGres 9.3.4,并且都以UTF8编码(Debian服务器上的语言环境也设置正确,因为我认为这会导致问题。)debian服务器的模板如下所示:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(3 rows)
认为我收到了一些错误的数据,我从CharFields转到TextFields没有限制,但仍然遇到错误 - 无论如何,它不是间歇性的,它是每个推特都会遇到异常。奇怪的是,它指定了类型字符变化(25)',这是我有一段时间的限制的旧版本 - 但我已经删除了数据库(多次...)在psql中检查是否在服务器上部署了正确版本的代码。
I had a similar problem to this调试的人,因为我不知道哪个字段引发了错误,但是在psql中仔细检查后,我得到了表信息:
Table "public.harvester_tweet"
Column | Type | Modifiers
---------------------------+--------------------------+--------------------------------------------------------------
id | integer | not null default nextval('harvester_tweet_id_seq'::regclass)
truncated | boolean | not null
text | text | not null
in_reply_to_status_id | bigint |
favorite_count | integer | not null
author_id | integer | not null
_json | text | not null
source | character varying(25) | not null
retweeted | boolean | not null
coordinates | text |
entities | text |
in_reply_to_screen_name | character varying(25) |
id_str | character varying(25) | not null
retweet_count | integer | not null
favorited | boolean | not null
retweeted_status | text | not null
user | text |
geo | text |
in_reply_to_user_id_str | character varying(25) |
possibly_sensitive | boolean | not null
lang | character varying(5) | not null
created_at | timestamp with time zone | not null
in_reply_to_status_id_str | character varying(25) |
place | text |
Models.py
class Tweet(models.Model):
truncated=models.BooleanField(default=False)
text=models.TextField()
in_reply_to_status_id=models.BigIntegerField(blank=True, null=True, default=None)
favorite_count=models.IntegerField(default=0)
author = models.ForeignKey(User)
_json = models.TextField()
source=models.TextField()
retweeted=models.BooleanField(default=False)
coordinates = models.TextField(blank=True, null=True)
entities = models.TextField(blank=True, null=True)
in_reply_to_screen_name = models.TextField(blank=True, null=True)
id_str = models.TextField()
retweet_count = models.IntegerField(default=0)
favorited = models.BooleanField(default=False)
retweeted_status = models.TextField()
user = models.TextField(blank=True, null=True) #User is a dictionary in the response; here we take a serialised version
geo = models.TextField(blank=True, null=True)
in_reply_to_user_id_str = models.TextField(blank=True, null=True)
possibly_sensitive = models.BooleanField(default=False)
lang = models.TextField()
created_at = models.DateTimeField()
in_reply_to_status_id_str = models.TextField(null=True, blank=True)
place = models.TextField(blank=True, null=True)
def __unicode__(self):
return unicode("@" + (self.author.screen_name) + " " + str(self.created_at))
读取它的代码在这里:
def read_tweet(tweet_data, current_user):
import logging
logger = logging.getLogger('django')
from datetime import date, datetime
#print "Inside read_Tweet"
from harvester.models import Tweet
from django.core.exceptions import ObjectDoesNotExist
from django.db import DataError
#We might get weird results where user has changed their details"], so first we check the UID.
#print tweet_data["created_at"]
from dateutil.parser import parse
tweet_data["created_at"] = parse(tweet_data["created_at"])
try:
#print "trying tweet_data["id"
current_tweet =Tweet.objects.get(id_str=tweet_data["id_str"])
created=False
return current_user, created
except ObjectDoesNotExist:
pass
try:
current_tweet, created = Tweet.objects.get_or_create(
truncated=tweet_data["truncated"],
text=tweet_data["text"],
favorite_count=tweet_data["favorite_count"],
author = current_user,
_json = {},
source=tweet_data["source"],
retweeted=tweet_data["retweeted"],
coordinates = tweet_data["coordinates"],
entities = tweet_data["entities"],
in_reply_to_screen_name = tweet_data["in_reply_to_screen_name"],
id_str = tweet_data["id_str"],
retweet_count = tweet_data["retweet_count"],
favorited = tweet_data["favorited"],
user = tweet_data["user"],
geo = tweet_data["geo"],
in_reply_to_user_id_str = tweet_data["in_reply_to_user_id_str"],
lang = tweet_data["lang"],
created_at = tweet_data["created_at"],
place = tweet_data["place"])
print "DEBUG", current_user, current_tweet
return current_tweet, created
except DataError, e:
#Catchall to pick up non-parsed tweets
print "DEBUG ERROR", e, tweet_data
return None, False
我将数据库放在psql drop database name;
中,然后重新创建它,运行syncdb并在南方迁移(由于各种原因,我仍然在1.6上) - 为什么还存在?
编辑:我只是三重检查了这个,完全改变了数据库,用不同的名字创建了一个新的数据库,它仍然是这样的!我错过了什么?我已经进入服务器并查看了models.py,他们就像这里所示,我感到很困惑。
第二次编辑:进入并运行schemamigration后,它现在正在工作(哇!),但我仍然想了解这里的行为,因为它不是我的意思期待。