Skip to content
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

Primary key should be NOT NULL #23622

Merged
merged 1 commit into from
Apr 18, 2016

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Feb 11, 2016

Follow up to #18228.

In MySQL and PostgreSQL, primary key is to be NOT NULL implicitly.
But in SQLite it must be specified NOT NULL explicitly.

lib/active_record/connection_adapters/sqlite3_adapter.rb#L56

@rails-bot
Copy link

r? @schneems

(@rails-bot has picked a reviewer for you, use r? to override)

@sgrif
Copy link
Contributor

sgrif commented Feb 11, 2016

Given that this only affects SQLite, I would prefer an implementation that only touches SQLite related code. Is there a reason we can't just emit NOT NULL from SchemaCreation as part of the primary_key option in that specific adapter?

@kamipo
Copy link
Member Author

kamipo commented Feb 11, 2016

This change is also needed for #23553. Should we affect only sqlite3 and mysql2 adapters?

@sgrif
Copy link
Contributor

sgrif commented Feb 11, 2016

Why would this affect the mysql2 adapter? I thought primary keys were implicitly not null in mysql?

@kamipo
Copy link
Member Author

kamipo commented Feb 11, 2016

AR's column definition is default nullable. But the native timestamp type in MySQL is default not null.
To nullable, it is needed NULL attribute. https://github.com/rails/rails/pull/23553/files#diff-763a0bf062db014c3fd65f85798e045aR33

NULL attribute conflicts with PRIMARY KEY.

@kamipo kamipo force-pushed the primary_key_should_be_not_null branch from 2c91c68 to 0faa08b Compare February 12, 2016 00:19

def column_options(o)
options = super
options[:null] = false if o.primary_key
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed to only affect to SQLite, thanks!

@kamipo
Copy link
Member Author

kamipo commented Feb 12, 2016

r? @sgrif

@rails-bot rails-bot assigned sgrif and unassigned schneems Feb 12, 2016
@kamipo kamipo force-pushed the primary_key_should_be_not_null branch 4 times, most recently from 5bc47b2 to 23f6452 Compare March 2, 2016 00:34
@@ -229,7 +229,7 @@ def test_any_type_primary_key
assert_equal "code", Barcode.primary_key

column = Barcode.column_for_attribute(Barcode.primary_key)
assert_not column.null unless current_adapter?(:SQLite3Adapter)
assert_not column.null
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test first at #23959.
This PR fixes the wrong behavior for sqlite3 adapter.

@kamipo kamipo force-pushed the primary_key_should_be_not_null branch 3 times, most recently from 62b3e49 to 0ea47eb Compare March 11, 2016 06:17
@kamipo
Copy link
Member Author

kamipo commented Mar 11, 2016

@rafaelfranca What do you think about this?

Follow up to rails#18228.

In MySQL and PostgreSQL, primary key is to be `NOT NULL` implicitly.
But in SQLite it must be specified `NOT NULL` explicitly.
@kamipo kamipo force-pushed the primary_key_should_be_not_null branch from 0ea47eb to 98fb374 Compare March 12, 2016 09:27
@kamipo
Copy link
Member Author

kamipo commented Apr 18, 2016

@jeremy What do you think about this?

@jeremy jeremy merged commit 98fb374 into rails:master Apr 18, 2016
jeremy added a commit that referenced this pull request Apr 18, 2016
@kamipo kamipo deleted the primary_key_should_be_not_null branch April 18, 2016 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants