[Rails] Introspect "not null" for a Column?

G

Gavin Kistner

Using Ruby on Rails, how can I tell if the DB schema allows a
particular column in a table to be null or not? I don't see it in the
attributes of a Column object.

For example, this PSQL table:

create table users (
id serial primary key,
user varchar(200) not null,
passcrypt varchar(15) not null,
disabled_flag boolean default '0',
zip varchar(10) not null,
firstname varchar(50),
lastname varchar(50),
nickname varchar(50),
email varchar(50),
phone varchar(25)
);

produces the following columns through introspection:

#<ActiveRecord::ConnectionAdapters::Column:0x24c9854 @type=:boolean,
@limit=nil, @default="f", @name="disabled_flag">
#<ActiveRecord::ConnectionAdapters::Column:0x24c9818 @type=:integer,
@limit=nil, @default=nil, @name="id">
#<ActiveRecord::ConnectionAdapters::Column:0x24c97b4 @type=:string,
@limit=nil, @default=nil, @name="password">
#<ActiveRecord::ConnectionAdapters::Column:0x24c9750 @type=:string,
@limit=25, @default=nil, @name="phone">
#<ActiveRecord::ConnectionAdapters::Column:0x24c96ec @type=:string,
@limit=50, @default=nil, @name="email">
#<ActiveRecord::ConnectionAdapters::Column:0x24c9660 @type=:string,
@limit=50, @default=nil, @name="nickname">
#<ActiveRecord::ConnectionAdapters::Column:0x24c95d4 @type=:string,
@limit=50, @default=nil, @name="lastname">
#<ActiveRecord::ConnectionAdapters::Column:0x24c9548 @type=:string,
@limit=50, @default=nil, @name="firstname">
#<ActiveRecord::ConnectionAdapters::Column:0x24c94bc @type=:string,
@limit=10, @default=nil, @name="zip">
#<ActiveRecord::ConnectionAdapters::Column:0x24c9430 @type=:string,
@limit=200, @default=nil, @name="login">

Note that the last 5 columns (in the sql) may be null, but are not
differentiated as such in the Column instances.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top