Django中數據庫操作相關的錯誤


問題:字段修改屬性發生錯誤

1>

>python manage.py makemigrations You are trying to add a non-nullable field 'price_monthly' to product without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows) 2) Quit, and let me add a default in models.py Select an option: 

{這個可能是之前已創建了表中的一條記錄,之后模型中增加了一個非空的字段,但是原來已經存在的記錄沒有這個值}

 

2>

 

>python manage.py migrate ... ... raise errorclass(errno, errorvalue)django.db.utils.ProgrammingError: (1146, "Table 'lab_data.bigdata_postgraduate_research_directions' doesn't exist")

{這個是因為在字段中添加了blank=True或者 null=True引起的}

 

 

3>

>python manage.py makemigrations
You are trying to change the nullable field 'job_title' on professor to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Ignore for now, and let me handle existing rows with NULL myself (e.g. adding a RunPython or RunSQL operation in the new migration file before the AlterField operation)
 3) Quit, and let me add a default in models.py
Select an option:

{這個是將模型中的null=True刪除了之后產生的錯誤}

1>原因解釋:

1. The migrations system is designed so that a single migration can be applied to more than one database. For example, you could have a development version, a staging version, and one or more production versions. That's whymaking the migration is a distinct step from applying the migration, and whymakemgirations can't just look at the currently active database to see that it doesn't have any rows. What if you then try to apply the migration to a database that does?

The solution in your case is simple: since there are no rows, option 1 (setting a default on all existing rows) won't do anything at all. So choose option 1, and any value you like.

[Django 1.7.1 requires a Default value for field - but no entry is in database. Why?]

2. Django adds a default "id" field to every model, you don't need an extra "twitbot_id" in your model. If a surrogate primary key is all you need, forget about "twitbot_id" because it will be a duplicate of the auto-generated "id". Seehttps://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields

If you add this and you already have TwitterBot objects in your database you must provide a default value to populate this column for existing rows in the database.

[Can't seem to lose this error: “You are trying to add a non-nullable field”]

3. 如果你跟我一樣是因為之前建好表a后,又創建一個表b作為a的父類,a中只有pass,那么因為表a已經創建,其中有數據,當a遷移時就會出現新表不能為null且沒有指定默認值時就會出現這種錯誤。

解決方案:

1>在基類b中添加允許為空或者添加默認值,並設置b不建表(meta中的abstract = true)

class Base(models.Model):
    '''
    基類
    '''
    title = models.CharField(max_length=150, null=True)
    content = models.TextField(null=True)
    time_stamp = models.DateTimeField(auto_now_add=True, default=timezone.now())
    link = models.URLField(blank=True, verbose_name='url_link')

    class Meta:
        abstract = True

Note:DataTimeField好像與其它的不一樣,不好改!

1>2>3>

刪除所有migrate文件(不用移除整個文件夾),然后重來

 

 

問題:manytomanyfeild沒有默認值

 

django admin gives warning “Field 'X' doesn't have a default value”

 

 

 

問題:添加元屬性發生錯誤

 

raise InternalError(errno, errorvalue)
django.db.utils.InternalError: (1017, "Can't find file: '.\\lab_data\\people_patent_prizes.frm' (errno: 2 -No such file or directory)")

{模型類中增加class Meta:db_table='People'使數據庫中對應的表名修改成了People,原來的表間聯系可能破壞了}
解決方案:

 

刪除所有migrate文件(不用移除整個文件夾),然后重來

http://blog.csdn.net/pipisorry/article/details/45727309

 

問題:表中字段不存在

"Unknown column 'name' in 'field list'"

django中創建了表professor繼承了表people的字段,並且在后台可以看到,但實際在數據庫中不存在(數據庫中查詢可看到)

出現問題原因:

1. model中編輯的字段沒有在數據庫對應的表里創建(原因可能是字段是繼承自父類,出現的什么問題?)

數據庫中查看表中的字段:

2. migration文件出了什么問題?導致沒有同步到數據庫(表都沒創建)

解決方案1:

在數據庫中手動添加沒有創建的字段

alter table bigdata_professor add column name varchar(6);

再次查看表中字段:

再次運行django服務器,后台添加name字段時就不會出錯了。

解決方案2:

先刪除整個migrations文件夾,再Python manage.py makemigrations,再python manage.py migrate


這樣表就可以重新建立成功了!(可以查詢到django中新建的表bigdata_professor....)


Note:

1. 成功后最好把之前刪除的文件夾migrations重新建一個(app中的)

2. 只刪除migration文件可能不會出現這個問題:

No migrations to apply.   Your models have changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

我了個去,都不知道為啥migration文件會出問題,刪除后再操作就沒事了,可能是(在makemigrations)之前先進行了migrate操作?

[Django Models (1054, “Unknown column in 'field list'”)]

 

 

問題:表不存在或者No migrations to apply

 

 "Table 'lab_data.bigdata_resdir' doesn't exist"

模型中建立新表后,makemigrations成功,但是migrate出現錯誤:

 

python manage.py migrate Operations to ...:
Apply all migrations: ...
No migrations to apply.(即使實際上明明makemigrations成功,並且有許多migrations可以應用)
  Your models have changes that are not yet reflected in a migration, and so won't be applied.  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

按照提示重新makemigration后migration文件就不會創建新表了,在數據庫中表也的確沒有新建。

 

原因:

 

1. Sounds like your initial migration was faked because the table already existed (probably with an outdated schema):

https://docs.djangoproject.com/en/1.7/topics/migrations/#adding-migrations-to-apps

"This will make a new initial migration for your app. Now, when you run migrate,Django will detect that you have an initial migration and that the tables it wants to create already exist, and will mark the migration as already applied."

Otherwise you would get an no-such-table error.

[No migrations to apply, even though there are migrations to apply]

2. 也可能是之前按照某個說明執行了一次python manage.py migrate --fake導致的。--fake 的含義是不執行該遷移腳本但是標記該腳本已經被執行過。導致之后無法正常進行遷移。

[Django 1.7 中 migrate 無法執行而且表不存在的解決方案]

解決方案:

方法1.

1> In MySQL Database delete row 'app_name' from the table 'django_migrations'.

打開mysql command line client, 進入創建好的數據庫use databasename; 查看表select * from django_migration; 發現將要執行的遷移腳本的 id 已經添加在表中了,將其刪除即可,即刪除最新一次app_name對就的id行。

2> Delete all migration files in migrations folder.

3> Try again python manage.py makemigrations and python manage.py migrate command.

[Django 1.7 - “No migrations to apply” when run migrate after makemigrations]

 

方法2:

 

移除整個migrations文件夾,重新makemigrations和migrate。之后就會自動創建了:

方法3:

實在不行,只能drop database,再重新建立了。

http://blog.csdn.net/pipisorry/article/details/45727309

 

問題:外鍵修改成多對多錯誤

ValueError: Cannot alter field bigdata.Postgraduate.publisher into bigdata.Postgraduate.publisher - they are not compatible types (you cannot alter to or from M2M fields
, or add or remove through= on M2M fields)

{這個錯誤是由將模型Postgraduate中的publisher字段從ForeignKey修改成ManyToManyField引起的}

解決方案:

刪除所有migrations文件,重新makemigrations和migrate

[foreignkey error: Django migration error :you cannot alter to or from M2M fields, or add or remove through= on M2M fields]

 

數據庫注冊到site管理錯誤

TypeError: __init__() missing 2 required positional arguments : 'model' and 'admin_site'

 

class DirectionsInline(inlineBase, admin.ModelAdmin):
    model = Directions
    inlines = [ImagesInline, ]
admin.site.register(Directions, DirectionsInline)

解決:原因可能是繼承admin.ModelAdmin的類中不能有model = ***

 

 

 

數據庫權限錯誤

 

django.db.utils.operationalerror:<1045,"access denied for user root@localhost using password yes>

解決方案1:django setting.py文件中設置的database用戶名或者密碼錯了,修改一下就可以了

或者是django運行項目時用的不是settings.py文件,這個在os.environ.setdefault("DJANGO_SETTINGS_MODULE", "labsite.settings")中設置

其它方案

Access denied for user 'root'@'localhost' (using password: YES)

mysql Access denied for user root@localhost錯誤解決方法總結(轉)

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

MySQL Forums ::Install & Repo ::ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

django.db.utils.operationalerror:<2003, "can't connect to mysql server on '127.0.0.1'(winerror 10061] No connection could be made because the target machine actively refused it)")

settings.py中設置的host和port如下

'HOST': '127.0.0.1','PORT': '3306'

如改動port為其它,可能導致該問題

 

 

 

其它問題:

1.Error: Tablespace for table xxx exists. Please DISCARD the tablespace before IMPORT

2. django.db.utils.ProgrammingError: (1146, "Table 'lab_data.django_migrations' doesn't exist")

3.django.db.utils.InternalError: (1050, "Table '`l ab_data`.`django_migrations`'already exists")

1>兩個模型的數據庫表名設置成一樣的了

class Meta:
    db_table = 'WorkExp1'

2>python manage.py migrate --fake

 

 

本文轉自:http://blog.csdn.net/pipisorry/article/details/45727309


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM