Setting default value for Foreign Key attribute

I would modify @vault’s answer above slightly (this may be a new feature). It is definitely desirable to refer to the field by a natural name. However instead of overriding the Manager I would simply use the to_field param of ForeignKey: class Country(models.Model): sigla = models.CharField(max_length=5, unique=True) def __unicode__(self): return u’%s’ % self.sigla class City(models.Model): … Read more

MySQL DROP all tables, ignoring foreign keys

I found the generated set of drop statements useful, and recommend these tweaks: Limit the generated drops to your database like this: SELECT concat(‘DROP TABLE IF EXISTS `’, table_name, ‘`;’) FROM information_schema.tables WHERE table_schema=”MyDatabaseName”; Note 1: This does not execute the DROP statements, it just gives you a list of them. You will need to … Read more

How do you enforce foreign key constraints in SQLite through Java?

Code like this: DriverManager.getConnection(“jdbc:sqlite:some.db;foreign keys=true;”) Does not work. You have to create org.sqlite.SQLiteConfig and set it as properties when call getConnection from DriverManager. public static final String DB_URL = “jdbc:sqlite:database.db”; public static final String DRIVER = “org.sqlite.JDBC”; public static Connection getConnection() throws ClassNotFoundException { Class.forName(DRIVER); Connection connection = null; try { SQLiteConfig config = new … Read more

Hibernate unidirectional one to many association – why is a join table better?

Consider the situation where the owned entity type can also be owned by another parent entity type. Do you put foreign key references in the owned table to both parent tables? What if you have three parent types? It just doesn’t scale to large designs. A join-table decouples the join, so that the owned table … Read more

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

How to use django models with foreign keys in different DBs?

Cross-database limitations Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database. Django – limitations-of-multiple-databases Trouble Same trouble. Bug in ForeignKey() … Read more