Rails SQL3 uniqueness validation failing












0















I am working on a site and I have created a migration file to make the name attribute for an item unique. I have added validations to check for uniqueness but all my tests are causing "ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed".



Here are my migration filess



class AddIndexToProduct < ActiveRecord::Migration[5.1]
def change
add_index :products, :name, unique: true
end
end

class CreateProducts < ActiveRecord::Migration[5.1]
def change
create_table :products do |t|
t.string :name
t.float :price
t.integer :inventory

t.timestamps
end
end
end


Here are my validations



  validates :name, presence: true, length: {maximum: 15}, uniqueness: {case_sensitive: false}
validates :price, presence: true, length: {maximum: 15}
validates :price, presence: true, numericality: {only_float: true, greater_than: 0, less_than: 10000}
validates :inventory, presence: true, numericality: {only_integer: true, greater_than: 0, less_than: 10000}


Here are my model tests



  def setup
@product = Product.new(name: "Bannana", price: 1.96, inventory: 3)
end

test "valid product" do
assert @product.valid?
end

test "products should be unique" do
duplicate_product = @product.dup
duplicate_product.name = @product.name.upcase
@product.save
assert_not duplicate_product.valid?
end

test "check if we can add a product with a negative price or zero" do
@product.price = -20
assert_not @product.valid?
@product.price = 0
assert_not @product.valid?
end

test "check if we can add a product without a name, price, or quantity" do
@product.name = " "
assert_not @product.valid?
@product.price = nil
assert_not @product.valid?
@product.inventory = nil
assert_not @product.valid?
end

test "check if name is too long" do
@product.name = "This name for a product is way too long for meeeee"
assert_not @product.valid?
end









share|improve this question

























  • have you check this thread stackoverflow.com/a/31095778/938947, possible can help

    – widjajayd
    Jan 19 at 16:16






  • 1





    I believe the issue was that my fixtures were being added to the DB before my validations were running

    – User
    Jan 19 at 17:48
















0















I am working on a site and I have created a migration file to make the name attribute for an item unique. I have added validations to check for uniqueness but all my tests are causing "ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed".



Here are my migration filess



class AddIndexToProduct < ActiveRecord::Migration[5.1]
def change
add_index :products, :name, unique: true
end
end

class CreateProducts < ActiveRecord::Migration[5.1]
def change
create_table :products do |t|
t.string :name
t.float :price
t.integer :inventory

t.timestamps
end
end
end


Here are my validations



  validates :name, presence: true, length: {maximum: 15}, uniqueness: {case_sensitive: false}
validates :price, presence: true, length: {maximum: 15}
validates :price, presence: true, numericality: {only_float: true, greater_than: 0, less_than: 10000}
validates :inventory, presence: true, numericality: {only_integer: true, greater_than: 0, less_than: 10000}


Here are my model tests



  def setup
@product = Product.new(name: "Bannana", price: 1.96, inventory: 3)
end

test "valid product" do
assert @product.valid?
end

test "products should be unique" do
duplicate_product = @product.dup
duplicate_product.name = @product.name.upcase
@product.save
assert_not duplicate_product.valid?
end

test "check if we can add a product with a negative price or zero" do
@product.price = -20
assert_not @product.valid?
@product.price = 0
assert_not @product.valid?
end

test "check if we can add a product without a name, price, or quantity" do
@product.name = " "
assert_not @product.valid?
@product.price = nil
assert_not @product.valid?
@product.inventory = nil
assert_not @product.valid?
end

test "check if name is too long" do
@product.name = "This name for a product is way too long for meeeee"
assert_not @product.valid?
end









share|improve this question

























  • have you check this thread stackoverflow.com/a/31095778/938947, possible can help

    – widjajayd
    Jan 19 at 16:16






  • 1





    I believe the issue was that my fixtures were being added to the DB before my validations were running

    – User
    Jan 19 at 17:48














0












0








0








I am working on a site and I have created a migration file to make the name attribute for an item unique. I have added validations to check for uniqueness but all my tests are causing "ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed".



Here are my migration filess



class AddIndexToProduct < ActiveRecord::Migration[5.1]
def change
add_index :products, :name, unique: true
end
end

class CreateProducts < ActiveRecord::Migration[5.1]
def change
create_table :products do |t|
t.string :name
t.float :price
t.integer :inventory

t.timestamps
end
end
end


Here are my validations



  validates :name, presence: true, length: {maximum: 15}, uniqueness: {case_sensitive: false}
validates :price, presence: true, length: {maximum: 15}
validates :price, presence: true, numericality: {only_float: true, greater_than: 0, less_than: 10000}
validates :inventory, presence: true, numericality: {only_integer: true, greater_than: 0, less_than: 10000}


Here are my model tests



  def setup
@product = Product.new(name: "Bannana", price: 1.96, inventory: 3)
end

test "valid product" do
assert @product.valid?
end

test "products should be unique" do
duplicate_product = @product.dup
duplicate_product.name = @product.name.upcase
@product.save
assert_not duplicate_product.valid?
end

test "check if we can add a product with a negative price or zero" do
@product.price = -20
assert_not @product.valid?
@product.price = 0
assert_not @product.valid?
end

test "check if we can add a product without a name, price, or quantity" do
@product.name = " "
assert_not @product.valid?
@product.price = nil
assert_not @product.valid?
@product.inventory = nil
assert_not @product.valid?
end

test "check if name is too long" do
@product.name = "This name for a product is way too long for meeeee"
assert_not @product.valid?
end









share|improve this question
















I am working on a site and I have created a migration file to make the name attribute for an item unique. I have added validations to check for uniqueness but all my tests are causing "ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed".



Here are my migration filess



class AddIndexToProduct < ActiveRecord::Migration[5.1]
def change
add_index :products, :name, unique: true
end
end

class CreateProducts < ActiveRecord::Migration[5.1]
def change
create_table :products do |t|
t.string :name
t.float :price
t.integer :inventory

t.timestamps
end
end
end


Here are my validations



  validates :name, presence: true, length: {maximum: 15}, uniqueness: {case_sensitive: false}
validates :price, presence: true, length: {maximum: 15}
validates :price, presence: true, numericality: {only_float: true, greater_than: 0, less_than: 10000}
validates :inventory, presence: true, numericality: {only_integer: true, greater_than: 0, less_than: 10000}


Here are my model tests



  def setup
@product = Product.new(name: "Bannana", price: 1.96, inventory: 3)
end

test "valid product" do
assert @product.valid?
end

test "products should be unique" do
duplicate_product = @product.dup
duplicate_product.name = @product.name.upcase
@product.save
assert_not duplicate_product.valid?
end

test "check if we can add a product with a negative price or zero" do
@product.price = -20
assert_not @product.valid?
@product.price = 0
assert_not @product.valid?
end

test "check if we can add a product without a name, price, or quantity" do
@product.name = " "
assert_not @product.valid?
@product.price = nil
assert_not @product.valid?
@product.inventory = nil
assert_not @product.valid?
end

test "check if name is too long" do
@product.name = "This name for a product is way too long for meeeee"
assert_not @product.valid?
end






sql ruby-on-rails






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 3:16









mu is too short

351k58688667




351k58688667










asked Jan 19 at 2:49









UserUser

1259




1259













  • have you check this thread stackoverflow.com/a/31095778/938947, possible can help

    – widjajayd
    Jan 19 at 16:16






  • 1





    I believe the issue was that my fixtures were being added to the DB before my validations were running

    – User
    Jan 19 at 17:48



















  • have you check this thread stackoverflow.com/a/31095778/938947, possible can help

    – widjajayd
    Jan 19 at 16:16






  • 1





    I believe the issue was that my fixtures were being added to the DB before my validations were running

    – User
    Jan 19 at 17:48

















have you check this thread stackoverflow.com/a/31095778/938947, possible can help

– widjajayd
Jan 19 at 16:16





have you check this thread stackoverflow.com/a/31095778/938947, possible can help

– widjajayd
Jan 19 at 16:16




1




1





I believe the issue was that my fixtures were being added to the DB before my validations were running

– User
Jan 19 at 17:48





I believe the issue was that my fixtures were being added to the DB before my validations were running

– User
Jan 19 at 17:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263680%2frails-sql3-uniqueness-validation-failing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263680%2frails-sql3-uniqueness-validation-failing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown