foreign-key-relationship
How to I show a list of ForeignKey reverse lookups in the DJango admin interface?
By default, a ModelAdmin will only let you manage the model “itself”, not related models. In order to edit the related Unit model, you need to define an “InlineModelAdmin” – such as admin.TabularInline – and attach it to your CustomerAdmin. https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects For example, in your admin.py: from django.contrib import admin from models import Customer, Unit … Read more
How to disable Constraints for all the tables and enable it?
EXEC sp_MSforeachtable @command1=”ALTER TABLE ? NOCHECK CONSTRAINT ALL” GO You may also want to do this: EXEC sp_MSforeachtable @command1=”ALTER TABLE ? DISABLE TRIGGER ALL” GO To enable them afterwards EXEC sp_MSforeachtable @command1=”ALTER TABLE ? ENABLE TRIGGER ALL” GO — SQL enable all constraints – enable all constraints sql server — sp_MSforeachtable is an undocumented system … Read more
How to duplicate schemas in PostgreSQL
You can probably do it from the command line without using files: pg_dump -U user –schema=”fromschema” database | sed ‘s/fromschmea/toschema/g’ | psql -U user -d database Note that this searches and replaces all occurrences of the string that is your schema name, so it may affect your data.
Postgres: left join with order by and limit 1
Use a dependent subquery with max() function in a join condition. Something like in this example: SELECT * FROM companies c LEFT JOIN relationship r ON c.company_id = r.company_id AND r.”begin” = ( SELECT max(“begin”) FROM relationship r1 WHERE c.company_id = r1.company_id ) INNER JOIN addresses a ON a.address_id = r.address_id demo: http://sqlfiddle.com/#!15/f80c6/2
Link in django admin to foreign key object
You can do the following: models.py (example): model B(models.Model): name = models.CharField(max_length=20) model A(models.Model): field1 = models.CharField(max_length=20) Bkey = models.ForeignKey(B) admin.py from django.core import urlresolvers class AAdmin(admin.ModelAdmin): list_display = [“field1″,”link_to_B”] def link_to_B(self, obj): link=urlresolvers.reverse(“admin:yourapp_b_change”, args=[obj.B.id]) #model name has to be lowercase return u'<a href=”https://stackoverflow.com/questions/28832897/%s”>%s</a>’ % (link,obj.B.name) link_to_B.allow_tags=True Replace yourapp with the name of your app.
Defining multiple Foreign Key for the Same table in Entity Framework Code First
To achieve what you want you need to provide some aditional configuration.Code First convention can identify bidirectional relationships, but not when there are multiple bidirectional relationships between two entities.You can add configuration (using Data Annotations or the Fluent API) to present this information to the model builder. With Data Annotations, you’ll use an annotation called … Read more
SQLite Foreign Key
You still have to create the column checklist_id INTEGER before you add it as a Foreign key. So it would be: CREATE TABLE checklist ( _id INTEGER PRIMARY KEY AUTOINCREMENT, checklist_title TEXT, description TEXT, created_on INTEGER, modified_on INTEGER ); CREATE TABLE item ( _id INTEGER PRIMARY KEY AUTOINCREMENT, checklist_id INTEGER, item_text TEXT, item_hint TEXT, item_order … Read more
Foreign key referencing a 2 columns primary key in SQL Server
Of course it’s possible to create a foreign key relationship to a compound (more than one column) primary key. You didn’t show us the statement you’re using to try and create that relationship – it should be something like: ALTER TABLE dbo.Content ADD CONSTRAINT FK_Content_Libraries FOREIGN KEY(LibraryID, Application) REFERENCES dbo.Libraries(ID, Application) Is that what you’re … Read more
SQL Sub queries in check constraint
It is not supported to look beyond the current row in a CHECK constraint. http://www.postgresql.org/docs/9.1/interactive/sql-createtable.html says: A check constraint specified as a column constraint should reference that column’s value only, while an expression appearing in a table constraint can reference multiple columns. Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns … Read more