# Db/schema.rb not preserving constraints

**URL:** <https://www.ruby-forum.com/t/db-schema-rb-not-preserving-constraints/135818>\
**Category:** Rails\
**Created:** [April 25, 2008, 9:23am UTC](https://www.ruby-forum.com/t/db-schema-rb-not-preserving-constraints/135818 "2008-04-25T09:23:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 25, 2008, 9:23am UTC](https://www.ruby-forum.com/t/db-schema-rb-not-preserving-constraints/135818/1 "2008-04-25T09:23:51Z")

</div>

Is there some magic that’s required to have db/schema.rb contain  
various execute() methods that exist in the migrations files?

For example, suppose in a migration I have the following:

### BEGIN

create\_table ‘foo’ do |t|  
t.text :name, :null =\> false  
end

execute “ALTER TABLE public.foo ADD CHECK(trim(name) = name AND  
length(name) \> 2)”

### END

Is there a way to preserve this extra DDL foo so that it’s preserved  
in db/schema.rb? As is, the rake test framework is arguably broken  
for PostgreSQL because the db/schema.rb is always recreated without  
the other DDL required to make a functional schema for the  
application.

Is that a fair assessment?

-sc

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 25, 2008, 9:25am UTC](https://www.ruby-forum.com/t/db-schema-rb-not-preserving-constraints/135818/2 "2008-04-25T09:25:38Z")

</div>

On 25 Apr 2008, at 03:36, Sean C. wrote:

> Is there some magic that’s required to have db/schema.rb contain  
> various execute() methods that exist in the migrations files?

Unfortunately not. Switching to the sql schema dumper should help (in  
environment.rb)

Fred

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 25, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/db-schema-rb-not-preserving-constraints/135818/3 "2008-04-25T14:52:25Z")

</div>

I’d also suggest that what you’re proposing to do is not a very  
‘Rails’ thing to do. One of the “strong opinions” of Rails is that  
you should consolidate your coding into a single language, and DB  
management should, if at all possible, take place in the application,  
_not_ the db. You could accomplish what you’re asking about as  
follows:

class Foo \< ARec::Base  
validates\_length\_of :name, :minimum=\>3  
before\_validation :trim\_name

protected  
def trim\_name  
name.strip  
end  
end

Certainly you’re free to do it as you’ve suggested and that may be  
most appropriate for your solution. However, some things that are  
difficult to do in Rails are difficult on purpose.

On Apr 25, 3:24 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 25, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/db-schema-rb-not-preserving-constraints/135818/4 "2008-04-25T18:47:33Z")

</div>

Setting:

Rails::Initializer.run do |config|  
config.active\_record.schema\_format = :sql  
end

in environment.rb did the trick… that and granting superuser privs  
to the test user. Thanks!

> I’d also suggest that what you’re proposing to do is not a very ‘Rails’ thing to do.

Oooh… yeah, I fundamentally reject that premise. In fact, Rails  
should default to using `pg_dump --schema-only` for generating schema  
for PostgreSQL, and likely doing the same for all database vendors.  
Rails’ schema dump isn’t complete and relying on it for completeness  
should horrify people.

> One of the “strong opinions” of Rails is that  
> you should consolidate your coding into a single language, and DB  
> management should, if at all possible, take place in the application,  
> _not_ the db.

I know that’s the opinion of Rails, but unfortunately MySQL brain rot  
has been successful in negatively impacting the design of Rails.  
validates\_\* is an organic hack to workaround MySQL deficiencies. I  
like that ruby checks data, but ruby can’t provide data guarantees -  
only the database can.

> However, some things that are  
> difficult to do in Rails are difficult on purpose.

Grr… “‘mother, may I,’ anti-foot shooting” is a design argument of  
convenience, not because of correctness, elegance, or completeness.

Just some outside perspective. -sc

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [January 23, 2015, 10:14pm UTC](https://www.ruby-forum.com/t/db-schema-rb-not-preserving-constraints/135818/5 "2015-01-23T22:14:07Z")

</div>

Using this gem: [https://github.com/vprokopchuk256/mv-core](https://github.com/vprokopchuk256/mv-core) you can do it  
in  
this way:

# def change create\_table :foo do |t| t.text :name, presence: true, custom: { statement: 'TRIM({name})

{name}', as: :check }  
end  
end

Best regards,  
Valeriy Prokopchuk

пʼятниця, 25 квітня 2008 р. 05:36:29 UTC+3 користувач Sean C.  
написав:

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 25, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/db-schema-rb-not-preserving-constraints/135818/6 "2008-04-25T22:02:24Z")

</div>

Wow. Can I add you to my Christmas card list?
