method
column_exists?
v6.1.3.1 -
Show latest stable
-
2 notes -
Class: ActiveRecord::ConnectionAdapters::SchemaStatements
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-4)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (38)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (31)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
column_exists?(table_name, column_name, type = nil, **options)
public
Checks to see if a column exists in a given table.
# Check a column exists column_exists?(:suppliers, :name) # Check a column exists of a particular type column_exists?(:suppliers, :name, :string) # Check a column exists with a specific definition column_exists?(:suppliers, :name, :string, limit: 100) column_exists?(:suppliers, :name, :string, default: 'default') column_exists?(:suppliers, :name, :string, null: false) column_exists?(:suppliers, :tax, :decimal, precision: 8, scale: 2)
Register or
log in
to add new notes.
stevo -
January 20, 2012
1 thank
Example
Check if id column exists in users table
ActiveRecord::Base.connection.column_exists?(:users, :id)
jonathonjones -
February 20, 2013
0 thanks
It also works with strings
It works with strings:
ActiveRecord::Base.connection.column_exists?('users', 'id')
Which is helpful if you need to specify the database/schema to use:
ActiveRecord::Base.connection.column_exists?('secondary.users', 'id')