So I have two models a CEO model:
class Ceo (models.Model):
first_name = models.CharField(max_length=63)
last_name = models.CharField(max_length=63)
website = models.URLField()
company_name = models.CharField(max_length=63, unique=False)
company_address = models.ForeignKey(Address)
ctime = models.DateTimeField(auto_now=True)
mtime = models.DateTimeField(auto_now_add=True)
Which was originally created with manage.py syncdb. I've since added a
model to track industries:
class Industry(models.Model):
name = models.CharField(max_length=63)
and a foreignkey field to Ceo:
industry = models.ForeignKey(Industry, null=True)
all is fine and good on the backend in the database (e.g. I
successfully migrated the db after the change). I'm trying to import
the industries to each ceo instance with the following code:
def update_ceo_industry(**kwargs):
ceo = Ceo.objects.get(**{
'first_name': kwargs['fname'],
'last_name': kwargs['lname'],
'website': kwargs['Website'],
'company_name': kwargs['Company'],
})
#I get the industry's name here and store it in industry
industry_obj, created = Industry.objects.get_or_create(**{'name':
unicode(industry)})
ceo.industry = industry_obj
ceo.save(force_update=True)
return
When I run this I can see that the industry is created, but once it
gets to the save function throws this error:
File "industry_getter.py", line 75, in update_ceo_industry
ceo.save(force_update=True)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py",
line 328, in save
self.save_base(force_insert=force_insert,
force_update=force_update)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py",
line 381, in save_base
raise DatabaseError("Forced update did not affect any rows.")
When I take out forced_update=True I get:
File "industry_getter.py", line 75, in update_ceo_industry
ceo.save()
File "/usr/lib/python2.5/site-packages/django/db/models/base.py",
line 328, in save
self.save_base(force_insert=force_insert,
force_update=force_update)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py",
line 400, in save_base
result = manager._insert(values, return_id=update_pk)
File "/usr/lib/python2.5/site-packages/django/db/models/manager.py",
line 144, in _insert
return insert_query(self.model, values, **kwargs)
File "/usr/lib/python2.5/site-packages/django/db/models/query.py",
line 1004, in insert_query
return query.execute_sql(return_id)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/
subqueries.py", line 317, in execute_sql
cursor = super(InsertQuery, self).execute_sql(None)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/
query.py", line 1974, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/util.py",
line 19, in execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: duplicate key value violates unique
constraint "finance_ceo_pkey"
Now the problem here is that if I try:
select * from finance_ceo where industry_id is not null;
I get 0 rows in either case. But the industry row is there:
select * from finance_industry; yields:
1 | Commercial Banks
So what's wrong with the lines:
ceo.industry = industry_obj
ceo.save(force_update=True)
Any thoughts?
--Jeff
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---