MySQL Django 模型中的大整数字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/283724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:22:09  来源:igfitidea点击:

Big integer field in django models

mysqldjango

提问by ayaz

In short: How do you specify a BIGINT in Django models?

简而言之:如何在 Django 模型中指定 BIGINT?

In a current project I am doing using Django (0.97-svn release), I've all the integer fields in all the models defined as IntegerField. It was working immaculately during the development cycle where I was using SQLite for the DB backend. However, as soon as I rolled off to MySQL, I noticed that for one of the IntegerFields, MySQL was truncating data---apparently, for reasons still unknown to me, SQLite didn't complain. I looked at the schema and found out that an IntegerField in Django is represented as an INT(11) field in MySQL. Naturally, the reason MySQL was truncating data was because the data was more than 11-digit in length. To workaround, I had to manually update the column to, in this case, be a BIGINT(20). Django and MySQL coexist peacefully with that. But whenever I reset the application in which rests the model containing that BIGINT, Django again creates it as an INT(11). I've looked at Django docs, and not found anything. Have I overlooked something?

在我使用 Django(0.97-svn 版本)执行的当前项目中,我将所有模型中的所有整数字段都定义为 IntegerField。在我将 SQLite 用于数据库后端的开发周期中,它运行良好。然而,当我开始使用 MySQL 时,我注意到对于其中一个 IntegerFields,MySQL 正在截断数据——显然,出于我仍然不知道的原因,SQLite 没有抱怨。我查看了架构,发现 Django 中的 IntegerField 在 MySQL 中表示为 INT(11) 字段。自然,MySQL 截断数据的原因是因为数据长度超过 11 位。要解决此问题,我必须手动将列更新为 BIGINT(20),在这种情况下。Django 和 MySQL 与之和平共处。但是每当我重置包含该 BIGINT 的模型的应用程序时,Django 再次将其创建为 INT(11)。我看过 Django 文档,但没有找到任何东西。我是否忽略了什么?

Thanks.

谢谢。

采纳答案by Javier

SQLite won't complain ever. it uses 'manifest typing', that is, the values have type, not the columns. It lets you store bigtext on a smallint collumn, or whatever you want! (except if you define an integer primary key, where it uses a 64-bit integer).

SQLite的不会抱怨不断。它使用“清单类型”,即值具有类型,而不是列。它允许您将 bigtext 存储在 smallint 列中,或者您想要的任何内容!(除非您定义了一个整数主键,它使用 64 位整数)。

that's a very convenient feature, but it makes a SQLite bad choice for developing if you're going to deploy with a different engine.

这是一个非常方便的功能,但如果您要使用不同的引擎进行部署,它会成为 SQLite 的错误选择。

for using a BIGINT, you'd have to create a custom field class. unfortunately, that part has changed on Django 1.0, so you'd have to rewrite it if/when you update.

要使用 BIGINT,您必须创建一个自定义字段类。不幸的是,该部分在 Django 1.0 上发生了变化,所以如果/当你更新时你必须重写它。

回答by kaleissin

BigIntegerFieldwas added in changeset 11887, 2009-12-17 09:10:38 and is part of Django 1.2 and newer.

BigIntegerField已添加到变更集 11887, 2009-12-17 09:10:38 中,并且是 Django 1.2 和更新版本的一部分。

回答by Javier

Try a DecimalField.

尝试一个 DecimalField。