1
Hello. I have two classes: User and Competition. I need to make a relationship between the two, as below:
public class User extends Model{
}
@Table(value = "competencias")
@BelongsTo(foreignKeyName = "user_id", parent = User.class)
public class Competencia extends Model{
}
CREATE TABLE users(
id integer NOT NULL DEFAULT nextval('operadores_id_seq'::regclass),
nome character varying(25) NOT NULL,
nome_completo character varying(60) NOT NULL,
email character varying(100) NOT NULL,
senha character varying(120) NOT NULL,
nivel integer NOT NULL DEFAULT 4,
ativo boolean,
created_at date DEFAULT now()
)
CREATE TABLE competencias(
id INTEGER NOT NULL DEFAULT NEXTVAL('competencias_id_seq'::regclass),
mes CHARACTER(2) NOT NULL,
ano CHARACTER(4) NOT NULL,
created_at DATE NOT NULL DEFAULT now(),
user_id INTEGER NOT NULL,
ativo BOOLEAN,
CONSTRAINT fk_competencia_operador FOREIGN KEY (user_id)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE NO ACTION
)
When commanding the relationship recording in the Competencies table, I get the error below:
competencia.add(user);
Exception in thread "AWT-EventQueue-0" org.javalite.activejdbc.associations.NotAssociatedException: No association from table 'competencias' to table 'users'
Translate your question colleague. Here is BR!
– Diego
Excuse me, I had posted it on activelite.io and Igor Polevoy asked me to paste it here. But it’s very simple, I see no difficulty in understanding.
– Luciano Bezerra
@Lucianobezerra Even if it’s easy to understand, [pt.so] is a site for Portuguese speakers, your question should be in Portuguese. If you prefer to leave in English, post no [so].
– Jéf Bueno
By the way, the problem is that you forgot to create Foreignkey.
– Jéf Bueno
I created, just forgot to paste the code: CONSTRAINT fk_competent_foreign KEY operator (user_id) REFERENCES public.users (id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE NO ACTION
– Luciano Bezerra
foreignKeyName = "user_id" CONSTRAINT fk_competency operator FOREIGN KEY (user_id) ?
– Luciano Bezerra