Closed ohadco closed 7 years ago
target = Tenant.find 5 # for example target.destroy # will destroy all associated records and tenant itself
On Tue, Oct 21, 2014 at 12:23 PM, ohadco notifications@github.com wrote:
Hi, When I delete tenant, the user, member and all it's records is still in the DB. I have added to tenants.rb model: has_many :members, dependent: :destroy has_many :all_other_models_acts_as_tenants, dependent: :destroy
I'm working with postgresql. I deleted the tenant from rails console and also directly from the pg-Admin gui. also tried to delete the member, the user etc.
So my question is what is the way to delete Tenant with all it's associated records?
Thanks!
— Reply to this email directly or view it on GitHub https://github.com/dsaronin/milia/issues/44.
Hi, Still doesn't delete the associated records. any ideas?
There is nothing the gem is doing to prevent the delete of associated records. Sounds more like a Rails issue. You might be having some behavior unique to being in the console... Try some experiments. On Oct 22, 2014 9:23 AM, "ohadco" notifications@github.com wrote:
Hi, Still doesn't delete the associated records. any ideas?
— Reply to this email directly or view it on GitHub https://github.com/dsaronin/milia/issues/44#issuecomment-60112566.
OK thank you.
one more question: when you wrote "has_many :all_other_models_acts_as_tenants, dependent: :destroy," did you mean that literally or symbollically?
in other words, for each association, you must actually write (in tenant.rb) has_many :posts, dependent: :destroy has_many :authors, dependent: :destroy has_one :star, dependent: :destroy
AND, in the corresponding models: belongs_to :tenant # in post.rb belongs_to :tenant # in author.rb belongs_to :tenant # in star.rb
etc.
I meant symbollically..
So I have this first part of your answer (has_many...) but I don't have the belongs_to part, because of that lines in the readme:
BUT: Do not add any belongs_to :tenant statements into any of your models. milia will do that for all. I do recommend, however, that you add into your app/models/tenant.rb file, one line per tenanted model such as the following (replacing with your model's name):
Do I need to add it manually?
Thanks
I forgot! You're correct, the gem does add that. No, don't put it in. On Oct 23, 2014 1:34 AM, "ohadco" notifications@github.com wrote:
I meant symbollically.. So I have this first part of your answer (has_many...) but I don't have the belongs_to part, because of that lines in the readme: BUT: Do not add any belongs_to :tenant statements into any of your models. milia will do that for all. I do recommend, however, that you add into your app/models/tenant.rb file, one line per tenanted model such as the following (replacing with your model's name):
Do I need to add it manually?
Thanks
— Reply to this email directly or view it on GitHub https://github.com/dsaronin/milia/issues/44#issuecomment-60208175.
I have just created a new model and it was added to it automatically, altho all other models don't have it. I suppose to see it in the other models themselves or it's milia hidden thing?
Thanks.
ב-23 באוק 2014, בשעה 15:39, Daudi Amani notifications@github.com כתב/ה:
I forgot! You're correct, the gem does add that. No, don't put it in. On Oct 23, 2014 1:34 AM, "ohadco" notifications@github.com wrote:
I meant symbollically.. So I have this first part of your answer (has_many...) but I don't have the belongs_to part, because of that lines in the readme: BUT: Do not add any belongs_to :tenant statements into any of your models. milia will do that for all. I do recommend, however, that you add into your app/models/tenant.rb file, one line per tenanted model such as the following (replacing with your model's name):
Do I need to add it manually?
Thanks
— Reply to this email directly or view it on GitHub https://github.com/dsaronin/milia/issues/44#issuecomment-60208175.
— Reply to this email directly or view it on GitHub.
I don't understand your question. You are also not supplying very much information. I prefer to see for myself rather than having it filtered through your interpretation. Use gist for log traces, source listings, etc.
best, David On Oct 25, 2014 1:01 AM, "ohadco" notifications@github.com wrote:
I have just created a new model and it was added to it automatically, altho all other models don't have it. I suppose to see it in the other models themselves or it's milia hidden thing?
Thanks.
ב-23 באוק 2014, בשעה 15:39, Daudi Amani notifications@github.com כתב/ה:
I forgot! You're correct, the gem does add that. No, don't put it in. On Oct 23, 2014 1:34 AM, "ohadco" notifications@github.com wrote:
I meant symbollically.. So I have this first part of your answer (has_many...) but I don't have the belongs_to part, because of that lines in the readme: BUT: Do not add any belongs_to :tenant statements into any of your models. milia will do that for all. I do recommend, however, that you add into your app/models/tenant.rb file, one line per tenanted model such as the following (replacing with your model's name):
Do I need to add it manually?
Thanks
— Reply to this email directly or view it on GitHub https://github.com/dsaronin/milia/issues/44#issuecomment-60208175.
— Reply to this email directly or view it on GitHub.
— Reply to this email directly or view it on GitHub https://github.com/dsaronin/milia/issues/44#issuecomment-60475226.
When destroying a tenant while using Postgres, it fails for tenanted models which already have tenanted records:
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "tenants" violates foreign key constraint "fk_rails_xyz" on table "members" DETAIL: Key (id)=(1) is still referenced from table "members". : DELETE FROM "tenants" WHERE "tenants"."id" = $1
I also tested this with a vanilla Sample Milia App (with pg
added to the gems and adapted database.yml
of course). May be related to Rails issue 12380.
Either destroy all members before destroying a tenant or add the following to tenant.rb
:
has_many :members, dependent: :destroy
Hope this helps!
Closing it for now. Feel free to add a note if you still have questions.
I did everything as taught above (has_many :members, dependent: :destroy
), and when I want to destroy I get:
D, [2019-07-18T21:41:30.079987 #4] DEBUG -- : Tenant::HABTM_Users Destroy (1.1ms) DELETE FROM "tenants_users" WHERE "tenants_users"."tenant_id" = $1 [["tenant_id", 25]]
D, [2019-07-18T21:41:30.084174 #4] DEBUG -- : Tenant Destroy (3.8ms) DELETE FROM "tenants" WHERE "tenants"."id" = $1 [["id", 25]]
D, [2019-07-18T21:41:30.085450 #4] DEBUG -- : (1.1ms) ROLLBACK
Traceback (most recent call last):
1: from (irb):2
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "tenants" violates foreign key constraint "fk_rails_4e4771d44b" on table "members")
DETAIL: Key (id)=(25) is still referenced from table "members".
: DELETE FROM "tenants" WHERE "tenants"."id" = $1
Hi, When I delete tenant, the user, member and all it's records is still in the DB. I have added to tenants.rb model:
has_many :members, dependent: :destroy
has_many :all_other_models_acts_as_tenants, dependent: :destroy
I'm working with postgresql. I deleted the tenant from rails console and also directly from the pg-Admin gui. also tried to delete the member, the user etc.
So my question is what is the way to delete Tenant with all it's associated records?
Thanks!