Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:21:05+00:00 2026-06-08T02:21:05+00:00

I’m writing migrations in Rails 3.2 using SQL (i.e. execute ) to enforce application

  • 0

I’m writing migrations in Rails 3.2 using SQL (i.e. execute) to enforce application logic (foreign keys, defaults, checks, triggers) at the database level. The reason is that I want consistent database interactions in case I need to use the same database from different applications (i.e. not Rails) or mass import data directly. Everything seems to work splendidly except for default values for one model (see below) .

For example created_at would include NOT NULL DEFAULT current_timestamp. This is all nice if I create a record in the database, but for some reason schema.rb doesn’t detect the default values, but correctly identifies the NOT NULL constraint. The consequence is that I can’t save an instance in the database using Model.create or Model.new (e.g. BibliographicItem.create(title: "Foo")), because created_at and updated_at end up being nil which violates the null: false constraint in schema.rb.

The offending table in schema.rb:

create_table "bibliographic_items", :id => false, :force => true do |t|
  t.string   "uuid",             :limit => nil, :null => false
  t.string   "title",            :limit => nil, :null => false
  t.integer  "publication_year"
  t.string   "type",             :limit => nil, :null => false
  t.text     "description"
  t.datetime "created_at",                      :null => false
  t.datetime "updated_at",                      :null => false
end

Its model:

class BibliographicItem < ActiveRecord::Base
  include Extensions::UUID
  attr_accessible :title, :publication_year, :description

  has_many :authorships
  has_many :authors, through: :authorships, order: "role DESC, author_order DESC NULLS LAST"

  validates :title, presence: true, length: { maximum: 500 }

  validates :publication_year, numericality: { only_integer: true,
                               less_than_or_equal_to: Date.today.year() }
end

The table creation in the execute statement:

CREATE TABLE bibliographic_items (
uuid uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
title varchar NOT NULL,
publication_year int CHECK (publication_year <= left(now()::text, 4)::int),
type varchar NOT NULL REFERENCES bibliographic_item_types ON UPDATE CASCADE ON DELETE RESTRICT,
description text,
created_at timestamp without time zone NOT NULL DEFAULT current_timestamp,
updated_at timestamp without time zone NOT NULL DEFAULT current_timestamp,
CHECK (updated_at >= created_at)
);

Why do .create and .new not assign values for created_at and updated_at? For all my other models, with similar (but simpler definitions) there is no issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:21:08+00:00Added an answer on June 8, 2026 at 2:21 am

    I found the cause of my problems, which as it happens, has nothing to do with SQL migrations, created_at or schema.rb, but is caused by failing the validation validates publication_year, numericality: { only_integer: true, less_than_or_equal_to: Date.today.year() } when publication_year is nil.

    Bangs head against wall.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.