简体   繁体   中英

Rails model user ID PG ERROR: NotNullViolation

I'm using rails 4, postgres 9.3 and devise, and at the moment of sign up an user, I'm getting the error of :

    PG::NotNullViolation: ERROR: the null value for column « usuario_id » violates the constraint not null. 

The primary key or ID of the table or model usuario is usuario_id and it's an integer.

I understand that it violates de constraint, but how can I autoincrement the ID without entering it through the sign up form?

I didn't do the migration 'cause it's unnecessary because I already have the tables in postgres. The only columns that I add are the devise ones. Anyway this is the table from postgresql.

                           Tabla «public.usuario»
    Column         |            Type            |                  Modifiers                   
------------------------+-----------------------------+---------------------------------
usuario_id             | integer                     | not null
comuna_id              | integer                     | not null
usuario_nombre         | character(256)              | 
usuario_apellidopat    | character(256)              | 
usuario_apellidomat    | character(256)              | 
usuario_rut            | character varying(1024)     | 
email                  | character varying(1024)     | 
usuario_nombre_usuario | character(256)              | 
password               | character varying(128)      | 
usuario_vip            | boolean                     | 
usuario_calle          | character varying(128)      | 
usuario_numero_calle   | smallint                    | 
usuario_villa          | character varying(128)      | 
usuario_numero_depto   | smallint                    | 
usuario_bloque         | smallint                    | 
encrypted_password     | character varying(255)      | not null valor por omisión      
reset_password_token   | character varying(255)      | 
reset_password_sent_at | timestamp without time zone | 
remember_created_at    | timestamp without time zone | 
sign_in_count          | integer                     | not null valor por omisión 0
current_sign_in_at     | timestamp without time zone | 
last_sign_in_at        | timestamp without time zone | 
current_sign_in_ip     | character varying(255)      | 
last_sign_in_ip        | character varying(255)      | 
confirmation_token     | character varying(255)      | 
confirmed_at           | timestamp without time zone | 
confirmation_sent_at   | timestamp without time zone | 
unconfirmed_email      | character varying(255)      | 
 Índexes:
"pk_usuario" PRIMARY KEY, btree (usuario_id)
"index_usuario_on_reset_password_token" UNIQUE, btree (reset_password_token)
"usuario_pk" UNIQUE, btree (usuario_id)
"relationship_34_fk" btree (comuna_id)
Foreign key constraints:
"fk_usuario_relations_comuna" FOREIGN KEY (comuna_id) REFERENCES comuna(comuna_id) ON UPDATE RESTRICT ON DELETE RESTRICT
Referenced by:
TABLE "compra_remate" CONSTRAINT "fk_compra_r_relations_usuario" FOREIGN KEY (usu_usuario_id) REFERENCES usuario(usuario_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "compra_remate" CONSTRAINT "fk_compra_r_relations_usuario2" FOREIGN KEY (usuario_id) REFERENCES usuario(usuario_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "compra_venta_especial" CONSTRAINT "fk_compra_v_relations_usuario" FOREIGN KEY (usu_usuario_id) REFERENCES usuario(usuario_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "compra_venta_normal" CONSTRAINT "fk_compra_v_relations_usuario" FOREIGN KEY (usu_usuario_id) REFERENCES usuario(usuario_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "compra_venta_especial" CONSTRAINT "fk_compra_v_relations_usuario2" FOREIGN KEY (usuario_id) REFERENCES usuario(usuario_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "compra_venta_normal" CONSTRAINT "fk_compra_v_relations_usuario2" FOREIGN KEY (usuario_id) REFERENCES usuario(usuario_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "notificacion" CONSTRAINT "fk_notifica_relations_usuario" FOREIGN KEY (usuario_id) REFERENCES usuario(usuario_id) ON UPDATE RESTRICT ON DELETE RESTRICT
TABLE "prod_of_nec" CONSTRAINT "fk_prod_of__relations_usuario" FOREIGN KEY (usuario_id) REFERENCES u:

First, in your User model migration, did change the primary key for the user table to be usuario_id ? See the following Stackoverflow question:

Second, did you make sure that you overrode the naming convention for the primary key in your ActiveRecord models?

Something like this:

class User < ActiveRecord::Base
  self.primary_key = "usuario_id"
end

See the following section in the Rails Guide for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM