简体   繁体   中英

Rails model test: ERROR: null value in column [..] violates not-null constraint

I'm building a Rails 4 (4.2.6) application and right now I want to specify some Minitest-tests for a 'place' model representing Points of interests getting printed on a Leaflet-Map. As DB we're using Postgres via 'pg' gem (which shall not be relevant for the test anyhow). Unfortunately even a simple test like the following results in an error when executing bundle exec rake test:models

class PlaceTest < ActiveSupport::TestCase
def setup
    @place = Place.new(latitude: 12, longitude: 52, name: 'foo', categories: 'bar')
end

test 'valid place is valid' do
    assert @place.valid?
end

The error is the following

PlaceTest#test_valid_place_is_valid:
ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR:  null value in column "latitude" violates not-null constraint
DETAIL:  Failing row contains (980190962, null, null, 2016-04-06 11:16:08, 2016-04-06 11:16:08, null, null).
: INSERT INTO "places" ("created_at", "updated_at", "id") VALUES ('2016-04-06 11:16:08', '2016-04-06 11:16:08', 980190962)

I have no clue how to interpret the error, since latitude is not null at all. Obviously it can't be a database related error since the instance is not being written into the db. Nevertheless the error message somehow has this sound. The schema.rb file looks like this:

ActiveRecord::Schema.define(version: 20160403170121) do

enable_extension "plpgsql"

create_table "descriptions", force: :cascade do |t|
  t.integer  "place_id"
  t.string   "language"
  t.text     "text"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

add_index "descriptions", ["place_id"], name: "index_descriptions_on_place_id", using: :btree

create_table "places", force: :cascade do |t|
  t.float    "latitude",   null: false
  t.float    "longitude",  null: false
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string   "name",       null: false
  t.string   "categories", null: false
end

  add_foreign_key "descriptions", "places"
end

By the way: I can perfectly instantiate a place object within the rails console and check if .valid? . I'd be glad if someone could help me interpret that error message, thanks in advance

Edit: I have not implemented any validations in my model file (actually sth I want to persue test-driven, only the tests don't work properly...)

Found the error, the corresponding YAML-file tried to spawn empty place objects. Problem solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM