Why do Rails migrations define foreign keys in the application but not in the database?

Rails holds some conventions that enforcement of data integrity should be done in the application, not in the database. For example, Rails even supports some database designs that cannot use foreign keys, such as Polymorphic Associations. Basically, Rails conventions have treated the database as a static data storage device, not an active RDBMS. Rails 2.0 … Read more

ForeignKey field related to abstract model in Django

It’s not possible to install Foreign Keys to abstract models in Django. You can however install Foreign Keys to a non abstract base class. The only limitation is that the reverse Foreign Key relation will return the base class instances. You can circumvent this limitation by using django-polymorphic. Django Polymorphic allows you to query the … Read more

How to alter “REFERENCES” in PostgreSQL?

Internal dependencies between tables and / or other objects are never bound to the object name. Internally, every object is stored in a catalog table and the OID (internal primary key) of the object is used for everything else. Accordingly, a FOREIGN KEY reference is stored in the catalog tables pg_constraint (the constraint itself incl. … Read more

Error Code: 1822. Failed to add the foreign key constaint. Missing index for constraint

create_user INT UNSIGNED ZEROFILL cannot reference id INT, because these count as different data types for purposes of foreign key reference. Make them the same data type. The only data type difference that is permitted between columns in a foreign key relationship is length of a varchar. For example, VARCHAR(10) can reference VARCHAR(20) or vice-versa. … Read more

sqlalchemy: create relations but without foreign key constraint in db?

Instead of defining “schema” level ForeignKey constraints create a custom foreign condition; pass what columns you’d like to use as “foreign keys” and the primaryjoin to relationship. You have to manually define the primaryjoin because: By default, this value is computed based on the foreign key relationships of the parent and child tables (or association … Read more

Foreign keys in django admin list display

I was looking for a solution to the same problem and ran across this question… ended up solving it myself. The OP might not be interested anymore but this could still be useful to someone. from functools import partial from django.forms import MediaDefiningClass class ModelAdminWithForeignKeyLinksMetaclass(MediaDefiningClass): def __getattr__(cls, name): def foreign_key_link(instance, field): target = getattr(instance, field) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)