-
Notifications
You must be signed in to change notification settings - Fork 21.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use :bigint for SQLite on references #31329
Comments
To be clear, on SQLite the primary key must be called exactly |
"A column of that type will always be 64-bit" is also misleading, since really it's based on the value, not the type. Any type that contains the characters "INT" in the type name are treated the same here (with the exception of the primary key which must have exactly the type name I'm not entirely sure that there's anything to be done here. It sounds like you may be getting confused by what Active Record gives to SQLite, which has little to no effect on the type itself. Can you provide more details on the actual application behavior that you're seeing which differs from what you expect? |
I would like |
I suspect the intent was to keep the foreign key the same as the primary key (which has to be called integer), leading to exporting :integer here (again, it literally doesn't matter what we do on SQLite here as long as it cobtsins the characters "int" somewhere) That said, I'll see if there was any particular intent here. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
@sgrif lets keep this alive. I have looked up to some references that may help you make an opinion on this:
|
for sqlite3 - the integer subtypes don't matter - they are all 8 bytes wide in memory https://sqlite.org/datatype3.html Forcing primary key to try using BIGINT is actually a problem in sqlite3 because 'INTEGER PRIMARY KEY' is an alias of ROWID, enabling autoincrement behavior. https://sqlite.org/autoinc.html I'm currently tracking down some annoyance when schema_dump from mysql and schema_load onto sqlite around this. |
Thats interesting info, thanks for sharing @onyxraven ❤️ |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
RailsBot reminded me. Here's the patch we put into place in our 5.0 codebase. We're in the process of going to 5.2, so we'll figure out what happens to this approach. # This patch is to work around a bug in Rails 5.0.x (and 5.1.x)
# Because we use SQLite for tests and MySQL in prod, the 'bigint' modifier for tables
# we've upgraded shows up in the 'create_table' statement in schema.rb. However,
# sqlite has special syntax about the primary key autoincrement - it must be specific
# and the 'bigint' type breaks it.
#
# related: https://github.com/rails/rails/issues/31329, https://github.com/rails/rails/pull/26266
#
# Because of this, we will strip the id type if it is bigint when create_table is called
# Here is the source of create_table, which is included on the specific connection adapters
# https://github.com/rails/rails/blob/5-0-stable/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb#L257
#
# This should be reevaluated for Rails 5.2 ++, which defaults _all_ new tables to bigint
# and changes how this patch would be applied
module TableTypeScrubber
def self.prepended(_base)
raise('Please re-validate the monkey patch for schema table type for Rails 5.2.x') if Gem::Version.new(Rails.version) >= Gem::Version.new(5.2)
end
def create_table(table_name, comment: nil, **options)
options.delete(:id) if options&.fetch(:id, nil) == :bigint
super(table_name, comment: comment, **options)
end
end
if ActiveRecord::Base.connection.adapter_name.downcase.start_with? 'sqlite'
module ActiveRecord
module ConnectionAdapters
class SQLite3Adapter
prepend TableTypeScrubber
end
end
end
end |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Any updates on this? I see that I think that it would be much better to always use |
Steps to reproduce
Discussing a possible documentation issue at #31321 with @sgrif the conclusion was that it was a bug at schema generation for SQLite.
When generating references, SQLite should also use
:bigint
type instead of:integer
, as it will be mapped later to INTEGER by the database, see doc. Rails edge is already usingbigint
when creatingactive_storage_blobs
table.To demonstrate this I have created https://github.com/albertoalmagro/test-references-sqllite3
To summarize this test application:
bigint
for:byte_size
:db/schema.rb
:As it can be seen
bigint
is used forbyte_syze
, while:integer
is used forrecord_id
andblob_id
.This screenshot is a capture from DB Browser for SQLite:
Expected behavior
References and primary key use also
:bigint
for SQLiteActual behavior
References and primary key use
:integer
for SQLiteSystem configuration
Rails version: Edge
Ruby version: 2.4.0
Please confirm bug. @sgrif could you also confirm please?
As I said at the above mentioned bug, once confirmed and clarified, I would like to write a fix for this.
Thanks!
The text was updated successfully, but these errors were encountered: