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
Join table cannot update or destroy throw error of no primary key #25347
Comments
cc @sgrif |
Tried reproducing this behavior with a demo app; could not reproduce the buggy behavior. The join tables can be updated and deleted as per normal expectation. The demo app is at https://github.com/prakashmurthy/join_table_issue_demo Tried with both 5.0.0 and 5.0.0.rc1. Ruby version: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15] Not sure what I am missing.... |
Experiencing the exact same problem, also with a joint table. Rails 5.0.0. Extract from db/schema.rb
@prakashmurthy's example does not use joint indices. Perhaps that is the root problem ? |
fwiw, I changed it to seperated indices and still have the same issue. |
Created a bug_report_template script for this issue; the behavior is not being reproduced by the script. Hopefully, it will help identify the difference that is causing the issue for @starkshaw and @ibussieres .
|
It works if I remove |
Active Record doesn't have built in support for composite primary keys. You'll need to remove the |
Actually if you can reproduce this by deleting through associations, there probably is a real issue here. Please update with a repro script if that's the case. |
happening for me on 5.0.0.1 |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Happening to me on Rails 5.0.2 |
Also happening to me on Rails 5.0.2 I have |
Hi @batmanbury ,
or you need to add ID column in a table which will serve as a primary_key. This solved my issue |
@sgrif
Here is attached template It works smooth if we remove |
I think I have the same problem...
Models:
Then I try the following on rails console:
On the first update attempt, nothing happens on sponsorships table. On the second update attemt I got: Any ideas? |
Per #25347 (comment):
That means you can't manipulate a model whose corresponding table doesn't have a single-column primary key defined. That includes doing so through an association that uses said model. |
Happens to on Rails 5.1.1 Migration class CreateUsersSubscriptions < ActiveRecord::Migration[5.1]
def change
create_join_table :users, :subscriptions do |t|
t.references :user, foreign_key: true
t.references :subscription, foreign_key: true
t.datetime :since_date
t.datetime :expire
t.timestamps
end
end
end rails console: 2.4.1 :010 > frk = SubscriptionsUser.create user: User.first, subscription: Subscription.first
User Load (1.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
Subscription Load (0.8ms) SELECT "subscriptions".* FROM "subscriptions" ORDER BY "subscriptions"."id" ASC LIMIT $1 [["LIMIT", 1]]
(0.5ms) BEGIN
SQL (0.7ms) INSERT INTO "subscriptions_users" ("user_id", "subscription_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) [["user_id", 1], ["subscription_id", 1], ["created_at", "2017-06-17 20:45:47.117825"], ["updated_at", "2017-06-17 20:45:47.117825"]]
(23.7ms) COMMIT
=> #<SubscriptionsUser user_id: 1, subscription_id: 1, since_date: nil, expire: nil, created_at: "2017-06-17 20:45:47", updated_at: "2017-06-17 20:45:47">
2.4.1 :011 > frk.destroy
(1.1ms) BEGIN
(1.1ms) ROLLBACK
NoMethodError: undefined method `to_sym' for nil:NilClass
from (irb):11 When I try to manually change schema_statements.rb:373 to $ rake db:migrate
== 20170617204034 CreateUsersSubscriptions: migrating =========================
-- create_join_table(:users, :subscriptions)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `to_sym' for true:TrueClass
Did you mean? to_s
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:313:in `column'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:162:in `primary_key'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/postgresql/schema_definitions.rb:56:in `primary_key'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:278:in `create_table'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:352:in `create_join_table'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:849:in `block in method_missing'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:818:in `block in say_with_time'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:818:in `say_with_time'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:838:in `method_missing'
/Users/mailo/Projects/my/webapps/subscriptions/subscriptions/db/migrate/20170617204034_create_users_subscriptions.rb:3:in `change'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:792:in `exec_migration'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:776:in `block (2 levels) in migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:775:in `block in migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:408:in `with_connection'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:774:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:953:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1230:in `block in execute_migration_in_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1298:in `block in ddl_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:225:in `block in transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:225:in `transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/transactions.rb:210:in `transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1298:in `ddl_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1229:in `execute_migration_in_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1201:in `block in migrate_without_lock'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1200:in `each'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1200:in `migrate_without_lock'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1148:in `block in migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1317:in `with_advisory_lock'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1148:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1007:in `up'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:985:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:171:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
NoMethodError: undefined method `to_sym' for true:TrueClass
Did you mean? to_s
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:313:in `column'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:162:in `primary_key'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/postgresql/schema_definitions.rb:56:in `primary_key'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:278:in `create_table'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:352:in `create_join_table'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:849:in `block in method_missing'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:818:in `block in say_with_time'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:818:in `say_with_time'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:838:in `method_missing'
/Users/mailo/Projects/my/webapps/subscriptions/subscriptions/db/migrate/20170617204034_create_users_subscriptions.rb:3:in `change'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:792:in `exec_migration'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:776:in `block (2 levels) in migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:775:in `block in migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:408:in `with_connection'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:774:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:953:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1230:in `block in execute_migration_in_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1298:in `block in ddl_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:225:in `block in transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:225:in `transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/transactions.rb:210:in `transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1298:in `ddl_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1229:in `execute_migration_in_transaction'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1201:in `block in migrate_without_lock'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1200:in `each'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1200:in `migrate_without_lock'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1148:in `block in migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1317:in `with_advisory_lock'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1148:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:1007:in `up'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/migration.rb:985:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:171:in `migrate'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/mailo/.rvm/gems/ruby-2.4.1@global/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace) |
I've read the thread and I can't wrap my head around the fact that
How can both be true? |
@GomaaK I'm in the same boat as you. I think if we upgraded to Rails 5 everything would be fine. |
I'm in Rails 5 in a new project and encountering this same issue. I echo @GomaaK's confusion. I don't have a model for these join tables; there's nowhere to even stick |
Having this issue on Rails 6.0. What are best practices here and are there plans to fix the AR behavior with join tables? Ideally join tables would not need a primary I'm switching to manually |
After being tired of these errors I just put a primary key on all my join tables |
The problem is still actual has_many :items, dependent: destroy Page.last.destroy => PG::Error: ERROR: zero-length delimited identifier at or near “”“” |
Old thread but in case it helps... my understanding is that if you use |
It works if you use |
Facing this on Rails 6.1.4.1 |
Steps to reproduce
Have models as follow:
Expected behavior
The join tables,
EventsUser
,DealsUser
,TeamUser
can be updated or deleted.Actual behavior
It can only be created, the update will throw
and destroy will throw
These following are my testing procedures in
rails c
.status
is an enum in model, and defined in migration as such:System configuration
Rails version:
Rails 5.0.0.rc1
Ruby version:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
Reference
#21304
The text was updated successfully, but these errors were encountered: