Enabling Foreign key constraints in SQLite

Finally figured this out from this post. The PRAGMA foreign_key setting does not persist but you can set it every time the connection is made in the ConnectionString. This allows you to use Visual Studio’s table adapters. Make sure you have the latest version (1.0.73.0) of system.data.sqlite installed (1.0.66.0 will not work). Change your ConnectionString … Read more

Is there a severe performance hit for using Foreign Keys in SQL Server?

There is a tiny performance hit on inserts, updates and deletes because the FK has to be checked. For an individual record this would normally be so slight as to be unnoticeable unless you start having a ridiculous number of FKs associated to the table (Clearly it takes longer to check 100 other tables than … Read more

SQLAlchemy: get Model from table name. This may imply appending some function to a metaclass constructor as far as I can see

Inspired by Eevee’s comment: def get_class_by_tablename(tablename): “””Return class reference mapped to table. :param tablename: String with name of table. :return: Class reference or None. “”” for c in Base._decl_class_registry.values(): if hasattr(c, ‘__tablename__’) and c.__tablename__ == tablename: return c

CONSTRAINT to check values from a remotely related table (via join etc.)

CHECK constraints cannot currently reference other tables. The manual: Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row. One way is to use a trigger like demonstrated by @Wolph. A clean solution without triggers: add redundant columns and include them in FOREIGN KEY constraints, which are the … Read more

Setting default value for Foreign Key attribute in Django

As already implied in @gareth’s answer, hard-coding a default id value might not always be the best idea: If the id value does not exist in the database, you’re in trouble. Even if that specific id value does exist, the corresponding object may change. In any case, when using a hard-coded id value, you’d have … Read more

Drop foreign key only if it exists

If you want to drop foreign key if it exists and do not want to use procedures you can do it this way (for MySQL) : set @var=if((SELECT true FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = ‘table_name’ AND CONSTRAINT_NAME = ‘fk_name’ AND CONSTRAINT_TYPE = ‘FOREIGN KEY’) = true,’ALTER TABLE table_name drop foreign key … Read more

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