Problem: The Django 1.7.8 table Users and Tenant do many-to-many associations. Django automatically creates a table users_tenants (corresponding to user_id and tenant_id) class users (models. Model): Tenants = models. Manytomanyfield (Tenant) when executing user.tenants.add (Tenant), reported the following issue: ERROR: (1452, ' cannot add or update a child row:a foreign K EY constraint fails (' iaasms '. ' users_tenants ', constraint ' users_tenants_users_id_3d9ac202e9485f18_fk_users_user_id ' FOREIGN key (' users_id ') REFERENCES ' users ' (' user_id ')) A: Although the foreign key check is turned off in the default in setting-DATABASES: ' OPTIONS ': {' Init_command ': ' SET foreign_key_checks = 0; ',} This error can be eliminated, but the result of the associated query User.tenants.all () is empty.
Root cause: After the first migrate, the length of the Users_tenants field tenant_id is derived from the length of tenant_id in table tenant. However, I later increased the length of the tenant_id in tenant, and after migrate, the length of tenant_id in users_tenants did not change. Therefore, the length of the tenant_id of users_tenants is not sufficient in the execution of User.tenants.add (tenant).
When the foreign key check is turned off in the default in setting-DATABASES, the result of the associated query is empty, although no error is made, but the value of Users_tenants's tenant_id is not complete (the actual data is greater than the length of the word).
WORKAROUND: Delete the migration files and databases and recreate the database.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.