foreign-keys

django : unique name for object within foreign-key set

I'm trying to upload files for an article model. Since an object can have multiple images, I'm using a foreign-key from file model to my article model. However, I want all the files to have unique titles. Herez the code snippet. class Article(models.Model): name = models.CharField(max_length=64) class Files(models.Model): titl...

How to change ordering in foreign key fields in django admin change_form

I have two tables, tasks and projects. And every task has a project (and every project can have N tasks). When adding or editing a task, projects are displayed as dropdown as it should, but dropdown is ordered by ID (or no ordering at all). Because i start to have plenty of projects, is there a way to get projects in a dropdown in alpha...

Adding Multiple Models to inlineformset_factory

I have a model like below. class Content(SimpleModel): title = models.CharField(max_length=255) body = models.TextField() slug = models.SlugField(max_length=50) def __unicode__(self): return self.title class MediumStuff(models.Model): meta_value = models.TextField() meta_key = models.SlugField('Fie...

Foreign keys in postgresql

Hi everyone, I've created some tables in postgres, added a foreign key from one table to another and set ON DELETE to CASCADE. Strangely enough, I have some fields that appear to be violating this constraint. Is this normal behaviour? And if so, is there a way to get the behaviour I want (no violations possible)? Edit: I orginaly cr...

How Would One Find a Dependency-Order list of Tables to Delete for Test Data?

I have a test suite that needs to delete any existing data in my local MySQL test instance for a large number (but not all) of the tables in the system. delete from customer; Of course, customer has quite a few foreign keys to it, so I also have to delete a few more tables... delete from customer_email; delete from customer_phone;...

Conditional Foreign Key to multiple tables

I have a table which contains two type of data, either for Company or Employee. Identifying that data by either 'C' or 'E' & a column storing primary key of it. So how can I give foreign key depending on data contained & maintain referential integrity dynamically. id | referenceid | documenttype ------------------------------- 1 | ...

PostgreSQL: How to constrain more than just existence on foreign key?

In PostgreSQL, what's the simplest way to enforce more than just existence on a foreign key? For example, given the following tables: create table "bar" ( bar_id serial primary key, status boolean not null ) create table "foo" ( foo_id serial primary key, bar_id integer references "bar" ) How could f...

Query is resulting in 1 row with null values when id is not found

The exact query: SELECT coupon_coupons.code, coupon_coupons.discountType AS 'type', coupon_coupons.discountAmount AS 'amount', coupon_coupons.discountApplied AS 'applied', coupon_coupons.description, group_concat(coupon_targetsku.sku separator ';') AS 'targetsku' FROM coupon_coupons LEFT ...

Enable foreign keys on Sqlite3 version 3.5.9

Hello. I'm trying to enable foreign keys on a sqlite3 database doing this: sqlite> PRAGMA foreign_keys=ON; But it doen't work. How can I do it? Note: the database is on an Android device. Thanks. ...

Foreign key null - performance degradation

I have table folder where column parent_id references on id if that folder has parent, if not then parent_id is null. Is that ok solution or I need extra table for this connection or other solution? Can foreign key be null at all, and if can is this solution will has bigger time execution ? table folder( id int primary key, ...

mysql simple problem with foreign keys

I have two tables: Table Author: ID (INTEGER), author_name (VARCHAR), first_name (VARCHAR), last_name (VARCHAR), preferred_name (VARCHAR). Table CoAuthored: ID (INTEGER) author1ID (INTEGER), author2ID (INTEGER), paper_ID (INTEGER) (ID of the co-authored paper referenced by this link) I want to add foreign key constraints such that aut...

Relational database foreign key constraints usage in practice

So you have a relational database and you define some foreign keys for reference integrity. But now, what's the better way to leverage them? Assume we are going to delete a row in a table with foreign keys to other tables. Use them as a garbage collector - Set all constraints to cascade. So in your application, you first check if the t...

MySQL foreign key to another foreign key

Hi. The idea is quite simple: i have three (or more) tables - master_tbl (id, something, somethingelse) - tbl2 (id, ....) - tbl3 (id, ....) Now what i want is a foreign key relationship, such as tbl3.id would point to tbl2.id and tbl2.id would point to master_tbl.id - all foreign keys are ON UPDATE CASCADE and ON DELETE CASCADE. What ...

2 XML Imports - Database design

SQL Server 2008. Morning, If someone could offer their advice it would be very much appreciated. Currently I’m looking to import two XML files, Donation amount and Donation Maker. The Donation Amount file would comprise of an Amount field and a DonationMakerID, and the Donation Maker file would comprise of a Name field and Donation...

How to figure out the Foreign Key Constraints of a MySQL table using Doctrine 1.1?

When two models are related to each other, Doctrine automatically generates foreign key constraints in the MySQL database beautifully. The default behaviour for onDelete and onUpdate RESTRICT, but can also be set to SET NULL or CASCADE. Now, I want to create a migration diff from an existing database compared to a YAML file. To do this,...

Foregin key constraint error but shouldn't be occuring

I have a list table with 5 entries in it, the first column in the identity column (PK), within a parent table is a column that is related to this list table by ID (FK). I am using C# in a web app to run an ExecuteScalar with a stored procedure to insert the items into the parent table. This is within a SQL database LIST TABLE: ID INT ...

Entity Framework Foreign Key Queries

I have two tables in my entity framework, objects, and parameters which have a foreign key pointing to the object to which they belong. I want to populate a tree with all the attributes of a certain object. So in order to find those I want to do this: String parentObject = "ParentObjectName"; var getAttributes = (from o in myDB....

"Can't adapt type" error on a Foreign key attribute with SQLAlchemy/PostGreSQL

I got a problem with PostGreSQL 8.4 and tables reflection. My metadata object seems to be ok (it has foreign keys, primary keys, every columns and tables). But when I try to associate an object to an another one through a Foreign key, I get : "sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'EventParameters' 'INSERT ...

MySQL Foreign Key Constraint - Integer Column

I have an integer column which I would like to add a foreign key constraint to. Only problem is if that column does not have/need a value, by default MySQL puts in a '0' value. This obviously breaks the foreign key constraint as there is no record in the primary table with a PK of 0. How can I overcome this problem? ...

Defining multiple foreign keys in one table to many tables

I have 3 models: Post: id title body Photo: id filepath Comment: id post_id body and corresponding tables in DB. Now, if I want to have comments only for my posts I can simply add following foreign key: ALTER TABLE comment ADD FOREIGN KEY (post_id) REFERENCES post (id). But I want to have comments for other models (photo, pr...