我有一个用户模型,其中包括一封电子邮件和一个university_id
我已经在我的模型文件中添加了以下行:
validates_uniqueness_of :email, :scope => [:university_id]
但是,当我尝试创建一个具有相同电子邮件但不同university_id的用户时,我收到以下错误消息:
ActiveRecord::RecordNotUnique in UsersController#create
SQLite3::ConstraintException: column email is not unique: INSERT INTO "
我有一个模型“产品”,存储( has_many“产品”)。我想验证产品名称的唯一性,但只在每个商店内。
现在我有这样的想法:
class Product < ActiveRecord::Base
belongs_to :store
validates_uniqueness_of :name, :scope => :store_id
end
现在,当我在任何产品上运行save时,我会得到:
SQLite3::ConstraintException: column name is not unique
我确信我在这里犯了一个简单的错误。请教我。
谢谢,
哈里斯
[woo:~/Development … e-roles/test] master(+7/-3) 23s ± rails test
Running via Spring preloader in process 18905
Run options: --seed 34925
# Running:
E
Error:
UserTest#test_User_must_have_an_email_address:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: user
我想编写一个测试,在删除记录时处理FOREIGN KEY constraint错误,但不会引发ConstraintException。
表的模式如下所示
class CreateCountries < ActiveRecord::Migration[5.1]
def change
create_table :countries do |t|
t.string :name
t.integer :sequence
end
end
end
class CreateStates < ActiveRecord::Migration[5.1]
我有一个重新排序的工作,创建一个艺术家,并将其与用户关联。用户艺术家:artists和艺术家has_and_belongs_to_many :users。
def self.perform(itunes_id, user_id=nil)
artist = Artist.find_by_itunes_id(itunes_id) || lookup_and_create_artist(itunes_id)
if user_id && user = User.find(user_id)
user.artists << artist
user.sav
我严格遵循了acl9自述文件,仔细检查了所有内容,但在尝试使用has_role!(:admin)时仍然出现以下错误。
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: roles_users.created_at may not be NULL: INSERT INTO "roles_users" ("user_id", "role_id") VALUES (1, 1)
唯一需要注意的是,我使用的是Rails3.2,并使用了以下错误修复:
有什么建议吗?
我创建了to表: class CreateWines < ActiveRecord::Migration[6.1]
def change
create_table :wines do |t|
t.string :name
t.string :producer
t.timestamps
end
end
end
class CreateWineItems < ActiveRecord::Migration[6.1]
def change
create_table :wine_items do |t|
我试着运行这个最简单的单元测试:
test "the truth" do
assert true
end
如下所示:
ruby -Itest test/unit/my_model_test.rb
(文件中只有一个测试,实际上是整个站点)
它会失败,并显示以下消息:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: column email is not unique: INSERT INTO "users"... [some values to insert here]
我正在尝试使用Devise in Rails设置一个两步注册过程,并遵循这个。当我在表单中输入电子邮件地址时(第一步),我得到一个错误,告诉我有一个路由错误,它找不到RegistrationsController
Started POST "/users" for 127.0.0.1 at 2012-05-03 22:50:59 -0400
ActionController::RoutingError (uninitialized constant RegistrationsController):
activesupport (3.2.1) lib/active_sup
这是我的static_pages_controller_test
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get static_pages_home_url
assert_response :success
end
test "should get help" do
get static_pages_help
有谁有处理XSD数据集抛出的ConstraintExceptions的技巧吗?
这是一条隐秘消息的例外:
System.Data.ConstraintException : Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
我正在转换我的rails 5.1应用程序,以开始使用UUID而不是增量ids作为默认的active_record。
我已将迁移文件更改为使用id: :uuid
class CreateProjects < ActiveRecord::Migration[5.1]
def change
create_table :projects, id: :uuid do |t|
t.string :title
t.text :description
t.timestamps
end
end
end
我添加了一个迁移文件来支持‘uuid’和&
我在使用DataTable时发现了这个错误。我向DataTable添加了一个主键列,然后向该表添加了一行,删除了该行,并向该表添加了具有相同键的行。这是可行的。当我试图对它调用RejectChanges()时,ConstraintException告诉我这个值已经存在。示例如下:
var dataTable = new DataTable();
var column = new DataColumn("ID", typeof(decimal));
dataTable.Columns.Add(column);
dataTable.PrimaryKe