尝试创建没有 id 列但面对“唯一索引”的表来自 sqlite 的错误
我按照下面链接中的说明“创建没有“id”列的表”,因为我使用的是“emp_id”。 创建没有 :id 列的 ActiveRecord 数据库表?
我遇到来自 Sqlite 的错误“表用户没有名为 id 的列:CREATE UNIQUE INDEX”。只是想知道您是否能为我提供一些建议。 非常感谢您的热情投入。
以下是原始迁移文件:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users, :id => false do |t|
t.integer :emp_id
t.string :name
t.integer :dept_id
t.timestamps
end
end
def self.down
drop_table :users
end
end
以下是运行“rake db:migrate”后的结果
-- add_index(:users, :id, {:unique=>true})
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table users has no column named id: CREATE UNIQUE INDEX "index_users_on_id" ON "users" ("id")
真诚的, 凯文·H
I followed the instruction on link below to "create table with no 'id' column", since i am using 'emp_id' instead.
Create an ActiveRecord database table with no :id column?
I am facing error "table users has no column named id: CREATE UNIQUE INDEX" from Sqlite. Just wondering if you could provide some suggestions to me.
Thank you so much for your kind input.
Below is the original migration file:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users, :id => false do |t|
t.integer :emp_id
t.string :name
t.integer :dept_id
t.timestamps
end
end
def self.down
drop_table :users
end
end
Below is the result after running "rake db:migrate"
-- add_index(:users, :id, {:unique=>true})
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table users has no column named id: CREATE UNIQUE INDEX "index_users_on_id" ON "users" ("id")
Sincerely,
Kevin H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ActiveRecord 期望所有支持模型的表(而不是关系,如 HTBTM 连接表)都有一个主键。您尝试做的似乎是 STI(单表继承),并且以另一种方式支持它(请参阅Rails 3 Way 中的单表继承)。
ActiveRecord expects a primary key for all tables that back a model (not a relationship, like a HTBTM–join table). What you're trying to do appears to be STI (single-table inheritance), and it's supported in another fashion (see Single Table Inheritance in The Rails 3 Way).