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

Discussion in 'Ruby' started by Gavin Kistner, Apr 4, 2005.

  1. 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.
    --
    (-, /\ \/ / /\/
     
    Gavin Kistner, Apr 4, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Fernandes
    Replies:
    9
    Views:
    627
    DHOLLINGSWORTH2
    Feb 27, 2005
  2. Davy
    Replies:
    1
    Views:
    988
    Steven D'Aprano
    Nov 22, 2007
  3. monojohnny
    Replies:
    3
    Views:
    89
    monojohnny
    Nov 4, 2008
  4. Bastian Ballmann

    Introspect imports from module

    Bastian Ballmann, Jun 21, 2012, in forum: Python
    Replies:
    0
    Views:
    250
    Bastian Ballmann
    Jun 21, 2012
  5. Bastian Ballmann

    Re: Introspect imports from module

    Bastian Ballmann, Jun 21, 2012, in forum: Python
    Replies:
    0
    Views:
    217
    Bastian Ballmann
    Jun 21, 2012
Loading...

Share This Page