# File lib/og/backends/psql.rb, line 241
        def create_table(klass)
                fields = create_fields(klass, TYPEMAP)

                sql = "CREATE TABLE #{klass::DBTABLE} (#{fields.join(', ')}"
                
                # Create table constrains
                
                if klass.__meta and constrains = klass.__meta[:sql_constrain]
                        sql << ", #{constrains.join(', ')}"
                end
                
                sql << ") WITHOUT OIDS;"
                
                # Create indices
                
                if klass.__meta and indices = klass.__meta[:sql_index]
                        for data in indices
                                idx, options = *data
                                idx = idx.to_s
                                pre_sql, post_sql = options[:pre], options[:post]
                                idxname = idx.gsub(/ /, "").gsub(/,/, "_").gsub(/\(.*\)/, "")
                                sql << " CREATE #{pre_sql} INDEX #{klass::DBTABLE}_#{idxname}_idx #{post_sql} ON #{klass::DBTABLE} (#{idx});"  
                        end
                end

                begin
                        exec(sql)
                        Logger.info "Created table '#{klass::DBTABLE}'."
                rescue => ex
                        # gmosx: any idea how to better test this?
                        if ex.to_s =~ /relation .* already exists/i
                                Logger.debug "Table already exists" if $DBG
                        else
                                raise
                        end
                end

                # create the sequence for this table. Even if the table
                # uses the oids_seq, attempt to create it. This makes
                # the system more fault tolerant.

                begin
                        exec "CREATE SEQUENCE #{klass::DBSEQ}"
                        Logger.info "Created sequence '#{klass::DBSEQ}'."    
                rescue => ex
                        # gmosx: any idea how to better test this?
                        if ex.to_s =~ /relation .* already exists/i
                                Logger.debug "Sequence already exists" if $DBG
                        else
                                raise
                        end
                end
                
                # Create join tables if needed. Join tables are used in
                # 'many_to_many' relations.
                
                if klass.__meta and joins = klass.__meta[:sql_join] 
                        for data in joins
                                # the class to join to and some options.
                                join_class, options = *data
                                
                                # gmosx: dont use DBTABLE here, perhaps the join class
                                # is not managed yet.
                                join_table = "#{self.class.join_table(klass, join_class)}"
                                join_src = "#{self.class.encode(klass)}_oid"
                                join_dst = "#{self.class.encode(join_class)}_oid"
                                begin
                                        exec "CREATE TABLE #{join_table} ( key1 integer NOT NULL, key2 integer NOT NULL )"
                                        exec "CREATE INDEX #{join_table}_key1_idx ON #{join_table} (key1)"
                                        exec "CREATE INDEX #{join_table}_key2_idx ON #{join_table} (key2)"
                                rescue => ex
                                        # gmosx: any idea how to better test this?
                                        if ex.to_s =~ /relation .* already exists/i
                                                Logger.debug "Join table already exists" if $DBG
                                        else
                                                raise
                                        end
                                end
                        end
                end
                
                begin
                        exec(sql)
                        Logger.info "Created join table '#{join_table}'."
                rescue => ex
                        # gmosx: any idea how to better test this?
                        if ex.to_s =~ /relation .* already exists/i
                                Logger.debug "Join table already exists" if $DBG
                        else
                                raise
                        end
                end
                
        end