【问题标题】:Rake db:reset has not updated change from integer to bigint; Sqlite supports bigint?Rake db:reset 没有更新从整数到大整数的变化; Sqlite 支持 bigint 吗?
【发布时间】:2015-05-15 05:43:51
【问题描述】:

我对正在发生的事情有一个假设……想问问社区。​​p>

我最近的迁移是根据 Heroku 上 PG 的要求将电话号码从 integer 更改为 bigint。我的架构文件已更新以反映最新的迁移(如日期匹配),仍然显示:

t.integer "phone"

因此,当我运行rake db:reset 时,电话仍然是integer,这对于 Heroku 中的 PG 来说是个问题,因为那时我会收到错误,因为integer 不支持我需要的值。

这可能只是反映了我的开发数据库是 sqlite 使用integer 作为每个文件的bigint 吗? http://www.sqlite.org/datatype3

我问这个只是为了确保数据库的其余部分是对齐的......我并不偏执于以某种方式错过了一堆其他迁移。

另一件事是,如果我的假设是正确的,那么我如何强制 bigint 作为架构中的定义以避免将来发生类似的事情?唯一的办法是让本地数据库也PG吗?

谢谢!

【问题讨论】:

  • 我认为将电话存储为字符串而不是 bigint 会更好
  • 无论如何,您确实应该在开发、测试和生产中使用相同的数据库。不要期望 ActiveRecord 提供很多有用的可移植性。

标签: ruby-on-rails database sqlite postgresql heroku


【解决方案1】:

来自http://sqlite.org/datatype3.html

存储在 SQLite 数据库中(或由数据库引擎操作)的每个值都具有以下存储类之一:

  • 整数。该值是一个有符号整数,根据值的大小存储在 1、2、3、4、6 或 8 个字节中。

因此假设您的绑定在适当的情况下使用sqlite3_bind_int64,SQLite 无缝支持 64 位整数。

【讨论】:

    【解决方案2】:

    简而言之:电话号码是文本,而不是数字。 Rails 比你更了解 SQLite。如果您想要一致的行为,请在开发、测试和生产中使用 PostgreSQL。

    首先,SQL bigint 是错误的电话号码数据类型。电话号码可以有前导零,但数字(包括 bigint)不能。

    其次,SQLite 甚至没有 SQL 意义上的数据类型。

    sqlite>     create table test (
       ...>       test_id integer
       ...>     );
    sqlite>     
    sqlite>     insert into test values ('wibble');
    sqlite>     select * from test;
    test_id   
    ----------
    wibble    
    

    第三,您可以在 SQLite 中使用 any 标记作为数据类型。什么都可以。想要一个名为“kate_mara”的“数据类型”吗?

    sqlite> drop table test;
    sqlite> create table test (
       ...>   n kate_mara
       ...> );
    sqlite> insert into test values (42);
    sqlite> select * from test;
    n                   
    --------------------
    42                  
    sqlite> .dump test
    PRAGMA foreign_keys=OFF;
    BEGIN TRANSACTION;
    CREATE TABLE test (
      n kate_mara
    );
    INSERT INTO "test" VALUES(42);
    COMMIT;
    

    第四,SQLite 的整数存储类可以存储 64 位的值。 (就此而言,kate_mara 也可以。)

    sqlite> drop table test;
    sqlite> create table test (
       ...>   n integer
       ...> );
    sqlite> insert into test values (0);
    sqlite> insert into test values (2147483647); -- Biggest 32-bit int
    sqlite> insert into test values (9223372036854775807); -- Biggest 64-bit int
    sqlite> .width 20
    sqlite> select * from test;
    n                   
    --------------------
    0                   
    2147483647          
    9223372036854775807 
    

    这是 SQLite 整数的上限。任何更高,它都会切换到浮点数而不会出现错误或警告。

    sqlite> update test set n = n + 1;
    sqlite> select * from test;
    n                   
    --------------------
    1                   
    2147483648          
    9.22337203685478e+18
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 2012-01-23
      • 2013-09-29
      • 1970-01-01
      • 2010-12-10
      • 2014-11-20
      • 2011-09-15
      • 2016-01-19
      相关资源
      最近更新 更多