I am creating one foreman plugin and my plugin creates additional tables
into the foreman database.
Some of tables using primary key (eg: Host :id) as foreign key in to my
plugins table(Eg: Xyz :host_id).
So, now i want to write relationship between foreman tables and my plugin
tables into the Model otherwise if i will not write these line then i am
getting exception about foreign key present when somebody deletes host from
foreman
:
Eg
and see it in action in foreman_salt, among others:
(loaded from an initialiser in engine.rb)
···
On 20/11/15 10:35, Aditya Gupta wrote:
> I am creating one foreman plugin and my plugin creates additional tables
> into the foreman database.
>
> Some of tables using primary key (eg: Host :id) as foreign key in to my
> plugins table(Eg: Xyz :host_id).
>
>
> So, now i want to write relationship between foreman tables and my
> plugin tables into the Model otherwise if i will not write these line
> then i am getting exception about foreign key present when somebody
> deletes host from foreman
> :
> Eg
>
> has_many :xyz, :foreign_key => :host_id, :dependent => :destroy
>
> above line will destroy all related foreign keys entry whenever
> respective primary key deleted from main table.
>
>
> But , in this case i will have to modify foreman code which i not good...
>
>
> So, is there any way where we can define such relationships into our
> custom plugin code .
Sorry, if i misunderstood you or might be i am not expert in term of plugin
development .
If somebody deletes host from All Hosts Menu item then after i am getting
error about the key is present in custom foreman table.
I want to establish the relationship in my custom model like whenever if
some body delete hosts from All hosts menu item of foreman then he/she can
able to delete and my table entries should delete too.
So, what should i write in my model or extension where it will
automatically delete host .
error:
Warning!ERROR: update or delete on table "hosts" violates foreign key
constraint "xyz_host_id_fk" on table "xyz" DETAIL: Key (id)=(12) is still
referenced from table "xyz"
Thanks,
Aditya
···
On Friday, November 20, 2015 at 4:14:13 PM UTC+5:30, Dominic Cleal wrote:
>
> On 20/11/15 10:35, Aditya Gupta wrote:
> > I am creating one foreman plugin and my plugin creates additional tables
> > into the foreman database.
> >
> > Some of tables using primary key (eg: Host :id) as foreign key in to my
> > plugins table(Eg: Xyz :host_id).
> >
> >
> > So, now i want to write relationship between foreman tables and my
> > plugin tables into the Model otherwise if i will not write these line
> > then i am getting exception about foreign key present when somebody
> > deletes host from foreman
> > :
> > Eg
> >
> > has_many :xyz, :foreign_key => :host_id, :dependent => :destroy
> >
> > above line will destroy all related foreign keys entry whenever
> > respective primary key deleted from main table.
> >
> >
> > But , in this case i will have to modify foreman code which i not
> good...
> >
> >
> > So, is there any way where we can define such relationships into our
> > custom plugin code .
>
> Yes, you can extend a model from a plugin and add statements such as
> has_many at the class level.
>
> Usually you do this inside the "included" block in a Rails concern, a
> standard module/mixin that can be added to Host::Managed:
>
>
> http://projects.theforeman.org/projects/foreman/wiki/How_to_Create_a_Plugin#Extend-Foreman-Model-Add-instance-or-class-methods
>
> and see it in action in foreman_salt, among others:
>
>
> https://github.com/theforeman/foreman_salt/blob/4.0.1/app/models/foreman_salt/concerns/host_managed_extensions.rb#L7
>
> https://github.com/theforeman/foreman_salt/blob/4.0.1/lib/foreman_salt/extensions.rb#L9-L10
> (loaded from an initialiser in engine.rb)
>
> --
> Dominic Cleal
> dom...@cleal.org
>
I think you had the has_many correct already. You need to create a
concern, like the one in the wiki link or the host_managed_extensions.rb
link and then add a line to the engine.rb to load it.
If that's not working, please share the code and error message you're
getting.
···
On 20/11/15 11:52, Aditya Gupta wrote:
>
>
> On Friday, November 20, 2015 at 4:14:13 PM UTC+5:30, Dominic Cleal wrote:
>
> On 20/11/15 10:35, Aditya Gupta wrote:
> > I am creating one foreman plugin and my plugin creates additional
> tables
> > into the foreman database.
> >
> > Some of tables using primary key (eg: Host :id) as foreign key in
> to my
> > plugins table(Eg: Xyz :host_id).
> >
> >
> > So, now i want to write relationship between foreman tables and my
> > plugin tables into the Model otherwise if i will not write these line
> > then i am getting exception about foreign key present when somebody
> > deletes host from foreman
> > :
> > Eg
> >
> > has_many :xyz, :foreign_key => :host_id, :dependent => :destroy
> >
> > above line will destroy all related foreign keys entry whenever
> > respective primary key deleted from main table.
> >
> >
> > But , in this case i will have to modify foreman code which i not
> good...
> >
> >
> > So, is there any way where we can define such relationships into our
> > custom plugin code .
>
> Yes, you can extend a model from a plugin and add statements such as
> has_many at the class level.
>
> Usually you do this inside the "included" block in a Rails concern, a
> standard module/mixin that can be added to Host::Managed:
>
> http://projects.theforeman.org/projects/foreman/wiki/How_to_Create_a_Plugin#Extend-Foreman-Model-Add-instance-or-class-methods
>
>
>
> and see it in action in foreman_salt, among others:
>
> https://github.com/theforeman/foreman_salt/blob/4.0.1/app/models/foreman_salt/concerns/host_managed_extensions.rb#L7
>
>
> https://github.com/theforeman/foreman_salt/blob/4.0.1/lib/foreman_salt/extensions.rb#L9-L10
>
>
> (loaded from an initialiser in engine.rb)
>
> --
> Dominic Cleal
> dom...@cleal.org
>
>
>
>
>
> Sorry, if i misunderstood you or might be i am not expert in term of
> plugin development .
>
> If somebody deletes host from All Hosts Menu item then after i am
> getting error about the key is present in custom foreman table.
>
> I want to establish the relationship in my custom model like whenever if
> some body delete hosts from All hosts menu item of foreman then he/she
> can able to delete and my table entries should delete too.
>
> So, what should i write in my model or extension where it will
> automatically delete host .
Thanks Alot Dominic for your valuable help… you saved lots of effort of
mine.
I will really forward to help you in future to contribute more in this
google group.
Thanks alot once again.
···
On Friday, November 20, 2015 at 7:41:16 PM UTC+5:30, Dominic Cleal wrote:
>
> On 20/11/15 11:52, Aditya Gupta wrote:
> >
> >
> > On Friday, November 20, 2015 at 4:14:13 PM UTC+5:30, Dominic Cleal > wrote:
> >
> > On 20/11/15 10:35, Aditya Gupta wrote:
> > > I am creating one foreman plugin and my plugin creates additional
> > tables
> > > into the foreman database.
> > >
> > > Some of tables using primary key (eg: Host :id) as foreign key in
> > to my
> > > plugins table(Eg: Xyz :host_id).
> > >
> > >
> > > So, now i want to write relationship between foreman tables and my
> > > plugin tables into the Model otherwise if i will not write these
> line
> > > then i am getting exception about foreign key present when
> somebody
> > > deletes host from foreman
> > > :
> > > Eg
> > >
> > > has_many :xyz, :foreign_key => :host_id, :dependent => :destroy
> > >
> > > above line will destroy all related foreign keys entry whenever
> > > respective primary key deleted from main table.
> > >
> > >
> > > But , in this case i will have to modify foreman code which i not
> > good...
> > >
> > >
> > > So, is there any way where we can define such relationships into
> our
> > > custom plugin code .
> >
> > Yes, you can extend a model from a plugin and add statements such as
> > has_many at the class level.
> >
> > Usually you do this inside the "included" block in a Rails concern,
> a
> > standard module/mixin that can be added to Host::Managed:
> >
> >
> http://projects.theforeman.org/projects/foreman/wiki/How_to_Create_a_Plugin#Extend-Foreman-Model-Add-instance-or-class-methods
> > <
> http://projects.theforeman.org/projects/foreman/wiki/How_to_Create_a_Plugin#Extend-Foreman-Model-Add-instance-or-class-methods>
>
> >
> >
> > and see it in action in foreman_salt, among others:
> >
> >
> https://github.com/theforeman/foreman_salt/blob/4.0.1/app/models/foreman_salt/concerns/host_managed_extensions.rb#L7
> > <
> https://github.com/theforeman/foreman_salt/blob/4.0.1/app/models/foreman_salt/concerns/host_managed_extensions.rb#L7>
>
> >
> >
> https://github.com/theforeman/foreman_salt/blob/4.0.1/lib/foreman_salt/extensions.rb#L9-L10
> > <
> https://github.com/theforeman/foreman_salt/blob/4.0.1/lib/foreman_salt/extensions.rb#L9-L10>
>
> >
> > (loaded from an initialiser in engine.rb)
> >
> > --
> > Dominic Cleal
> > dom...@cleal.org
> >
> >
> >
> >
> >
> > Sorry, if i misunderstood you or might be i am not expert in term of
> > plugin development .
> >
> > If somebody deletes host from All Hosts Menu item then after i am
> > getting error about the key is present in custom foreman table.
> >
> > I want to establish the relationship in my custom model like whenever if
> > some body delete hosts from All hosts menu item of foreman then he/she
> > can able to delete and my table entries should delete too.
> >
> > So, what should i write in my model or extension where it will
> > automatically delete host .
>
> I think you had the has_many correct already. You need to create a
> concern, like the one in the wiki link or the host_managed_extensions.rb
> link and then add a line to the engine.rb to load it.
>
> If that's not working, please share the code and error message you're
> getting.
>
> --
> Dominic Cleal
> dom...@cleal.org
>