- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 Devise 和 Paperclip 与 Rails 4 应用程序一起使用。
我正在尝试找出无法为同一用户添加另一个 pin 的原因。据我从错误中了解到,它具有用户独特性。我对吗?如何修复它?
登录后,我可以创建第一个带有图像和描述的图钉,但无法创建第二个图钉。我收到一个错误;
SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins" ("created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)
这是终端日志:
Started POST "/pins" for 127.0.0.1 at 2013-11-25 03:32:11 -0500
Processing by PinsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxxxxx", "pin"=>{"image"=>#<ActionDispatch::Http::UploadedFile:000000000000 @tempfile=#<Tempfile:/var/folders/z0/7gq1xnhs0051g0>, @original_filename="cheer_up.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"pin[image]\"; filename=\"cheer_up.png\"\r\nContent-Type: image/png\r\n">, "description"=>"cheer up!"}, "commit"=>"Create Pin"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 6 ORDER BY "users"."id" ASC LIMIT 1
(0.1ms) begin transaction
Binary data inserted for `string` type on column `image_content_type`
SQL (0.5ms) INSERT INTO "pins" ("created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Mon, 25 Nov 2013 08:32:11 UTC +00:00], ["description", "cheer up!"], ["image_content_type", "image/png"], ["image_file_name", "cheer_up.png"], ["image_file_size", 211523], ["image_updated_at", Mon, 25 Nov 2013 08:32:11 UTC +00:00], ["updated_at", Mon, 25 Nov 2013 08:32:11 UTC +00:00], ["user_id", 6]]
SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins" ("created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 6ms
ActiveRecord::RecordNotUnique (SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins" ("created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)):
app/controllers/pins_controller.rb:25:in `block in create'
app/controllers/pins_controller.rb:24:in `create'
这是我的 Pin 模型。
class Pin < ActiveRecord::Base
# associations
belongs_to :user
has_attached_file :image
# validations
validates_presence_of :description, :user_id
validates_attachment :image, presence: true,
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif' ] },
size: { less_than: 3.megabytes }
end
Devise 的用户模型
class User < ActiveRecord::Base
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
has_many :pins
#https://github.com/thoughtbot/paperclip#quick-start
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, #:recoverable
:rememberable, :trackable, :validatable
# https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-sign-in-using-their-username-or-email-address#tell-devise-to-use-login-in-the-authentication_keys
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first
else
where(conditions).first
end
end
end
这是我的架构
ActiveRecord::Schema.define(version: xxxxxxxxxx) do
create_table "pins", force: true do |t|
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
add_index "pins", ["user_id"], name: "index_pins_on_user_id", unique: true
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "username"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
add_index "users", ["username"], name: "index_users_on_username", unique: true
end
最佳答案
它失败是因为您的数据库中有唯一性约束:
查看架构或迁移文件:
add_index“pins”,[“user_id”],名称:“index_pins_on_user_id”,唯一:true
如果您认为不需要它,并且由于 User
模型中有一个 has_many :pins
,这似乎是错误的,您可以创建一个迁移到删除不必要的索引:
remove_index(:pins, :name => 'index_pins_on_user_id')
关于ruby - 为什么保存在这里失败,SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20188107/
我正在开发一些用于计费的数据库项目(PHP/MySQL)。 每当创建新账单时,我想生成一个由年、周和增量编号组成的账单编号。我想用触发器来做到这一点。触发器将使用现有的账单编号来查找增量编号,或者从新
我有一个 MySQL 插入,我正在使用 RAND 生成随机 INT 值问题是它不会插入到数据库中,因为该列接受 TINYINT ,如何将输出转换为 TINYINT。代码示例如下: INSERT INT
如果我想从单个插入中保存主键 (mytable_id),我已完成以下操作: CREATE OR REPLACE FUNCTION myfunct(ownerid text) RETURNS void
为了简单起见,假设我有两个表 用户表(id,email) 用户日志表(id, date) 无论 id 被插入到 user 表中,相同的 id 也应该被插入到 user_log 表中,否则事务应该失败。
为了简单起见,假设我有两个表 用户表(id,email) 用户日志表(id, date) 无论 id 被插入到 user 表中,相同的 id 也应该被插入到 user_log 表中,否则事务应该失败。
我知道在触发器中 - 至少对于 SQL Server - 人们永远不应该假设插入的表只有一行,这意味着触发器中这样的 SQL 通常是不好的: select @UserID = ID from inse
我正在使用 bigquery 对象中的方法 tabledata().insertAll 更新行列表。执行后,返回显示没有错误。但是,我的表仍然继续,没有写入任何数据。 可能是权限问题。如果是这样,为什
这是一个扩展 F# Recursive Tree Validation 的问题,我昨天已经很好地回答了。 这个问题涉及在现有树中插入一个 child 。这是我想使用的更新类型: type Name
我有 2 个表:用户和照片(在 mysql 数据库中)。 在这里你可以看到两个表之间的关系 User Photos -------------
我试图同时在不同的表上插入两行。 子查询INSERT INTO的AUTO_INCRMENT或id的值(如果已经存在)应该写入主查询中。 目前我有这个(仅用 3 个值简化),但它不起作用。我想知道是否有
我有一个 900 万行的表,由于其庞大的规模,我正在努力处理所有这些数据。 我想做的是在不覆盖数据的情况下将 IMPORT 一个 CSV 添加到表中。 在我做这样的事情之前; INSERT if no
我正在写新闻并将其插入到我的数据库中,我在 3 年前构建了代码并且运行良好,但我不能再插入了,我不明白为什么: $insert=mysqli_query($co,"INSERT INTO articl
我正在尝试编写一个简单的 INSERT 语句来将新用户添加到数据库中,但它不起作用,这意味着,我尝试插入到表中的数据都没有被插入。几个小时以来,我一直在尝试解决此问题,但没有成功。我尝试编写插入语句的
所以我有这个表格: http://i.imgur.com/vZYssQy.png 现在 ID、First Name、Last Name、DOB、Address、Phone Number 和 Post
在控制台中运行查询(SELECT 语句)时,从数据库检索到的数据以表格格式显示在数据库控制台工具窗口的结果 Pane 中。 我已经搜索过 datagrip Help我只是想知道是否有任何方法可以用于为
每当使用触发器插入行时,我都试图将另一行插入表中,但收到以下错误消息: The target table 'EDDSDBO.Redaction' of the DML statement cannot
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 6 年前。 Improve
我有以下代码片段: $get_data = mysqli_query ($connect, "SELECT * FROM users WHERE username = '$username'");
情况:需要向 SQLite 数据库中插入大量数据。 问题:我们可以使用两个语句来插入数据 - data = [("111", "222", "333"), ("AAA", "BBB", "CCC"),
我的数据库中有一个表 Teacher: TABLE Teacher ( ID CHAR (7) NOT NULL , name
我是一名优秀的程序员,十分优秀!