English 中文(简体)
Database trouble in Django: can t reset because of dependencies
原标题:
  • 时间:2009-11-09 18:38:32
  •  标签:
  • sql
  • django

I m trying to reset a database in Django, using:

python manage.py reset app

but get the following error:

Error: Error: app couldn t be reset. Possible reasons:
  * The database isn t running or isn t configured correctly.
  * At least one of the database tables doesn t exist.
  * The SQL was invalid.
Hint: Look at the output of  django-admin.py sqlreset app . That s the SQL this command wasn t able to run.
The full error: cannot drop table app_record because other objects depend on it
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

This is what my models.py file looks like:

class Record(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    year = models.IntegerField(blank=True, null=True)
    def __unicode__(self):
        return self.name

class Class(models.Model):
    record = models.ForeignKey(Record)
    def __unicode__(self):
        return self.id

I get that I need to use the DROP... CASCADE command in the SQL that deletes and recreates the database (the output of django-admin.py).

But how can I edit that SQL directly from models.py?


UPDATE

OK, I figured out how to delete tables manually (the database is postgres), have noted it here for anyone with the same problem:

python manage.py dbshell 
# drop table app_record cascade; 
# q 
python manage.py reset app 

Would still like to know if anyone has a better way to do it, though :)

最佳回答

The easy way to fully reset a Django database is using django-extensions.

It has a reset_db command that supports all Django s default database backends.

python manage.py reset_db

If you re using Django 1.2+ you should explicitly define the database you want to reset. If your project only uses one database, you should probably set --router=default

问题回答

I use a little unix pipeline that adds CASCADE to all the DROP statements.

python manage.py sqlreset myapp | sed  s/DROP TABLE (.*);/DROP TABLE 1 CASCADE;/g  | 
psql --username myusername mydbname

The problem of DROP TABLE CASCADE is that it just remove a foreign keys on related tables - after syncdb this relation is not recreated. I found no way to recreate the particular model s tables, so I m reseting whole application by recreating schema:

  DROP SCHEMA public CASCADE;
  CREATE SCHEMA "public" AUTHORIZATION "owner of database";

That should work only with database that supports schema, e.g. postgresql

Using the details in other answers, I made a bash function that I dropped into ~/.bash_profile (on Mac OS X).

django_reset () { python mainsite/manage.py sqlreset "$*" | sed  s/DROP TABLE (.*);/DROP TABLE 1 CASCADE;/g  | mainsite/manage.py dbshell ; }

Then just run this command in the terminal from your root code directory (so the path to mainsite/manage.py makes sense).

django_reset myappA myappB

And it ll execute!

I found another way. I m using sqlite3 that comes by default in Django. To reset table to default. python manage.py flush --database=default after this you will need to use the syncdb command again.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签