foreign-keys

Why aren't my sqlite3 foreign keys working?

拜拜、爱过 提交于 2019-11-30 04:49:42
问题 I run the following code from a python interpreter, and expect the insert statement to fail and throw some kind of exception. But it's not happening: Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sqlite3 >>> conn = sqlite3.connect("test.db") >>> conn.executescript(""" ... pragma foreign_keys=on; ... begin transaction; ... create table t1 (i integer primary key, a); ...

SQLAlchemy joins with composite foreign keys (with flask-sqlalchemy)

倖福魔咒の 提交于 2019-11-30 04:22:35
问题 I'm trying to understand how to do joins with composite foreign keys on SQLAlchemy and my attempts to do this are failing. I have the following model classes on my toy model (I'm using Flask-SQLAlchemy, but I'm not sure this has anything to do with the problem): # coding=utf-8 from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' db = SQLAlchemy(app) class Asset(db.Model): __tablename__ =

MySQL - Unique foreign key

爱⌒轻易说出口 提交于 2019-11-30 04:20:02
问题 I have to make one of the foreign keys unique. The problem is, I am getting the following message from the phpMyAdmin: The following indexes appear to be equal and one of them should be removed: consignmentnumber_id_UNIQUE, fk_consignments_consignmentnumbers2 So my question is this: should I be bothered? Is it really important not to have such indexes? 回答1: Every column with an key (primary, foreign) needs an index. Same with column being unique. You probably created two indexes (one when

Django admin inline: select_related

耗尽温柔 提交于 2019-11-30 04:11:54
Using Django 1.8 on Python 3.4.1 with models: class Product(models.Model): name = models.CharField(max_length=255) # some more fields here def __str__(self): return self.name class PricedProduct(models.Model): product = models.ForeignKey(Product, related_name='prices') # some more fields here def __str__(self): return str(self.product) class Coming(models.Model): # some unimportant fields here class ComingProducts(models.Model): coming = models.ForeignKey(Coming) priced_product = models.ForeignKey(PricedProduct) # more unimportant fields and the following admin.py: class ComingProductsInline

MySQL cannot create foreign key constraint

我是研究僧i 提交于 2019-11-30 04:10:42
I'm having some problems creating a foreign key to an existing table in a mysql database. I have the table exp : +-------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------+------+-----+---------+-------+ | EID | varchar(45) | NO | PRI | NULL | | | Comment | text | YES | | NULL | | | Initials | varchar(255) | NO | | NULL | | | ExpDate | date | NO | | NULL | | | InsertDate | date | NO | | NULL | | | inserted_by | int(11) unsigned | YES | MUL | NULL | | +-------------+------------------+------+-----+---------

PostgreSQL check constraint for foreign key condition

佐手、 提交于 2019-11-30 04:02:31
I have a table of users eg: create table "user" ( id serial primary key, name text not null, superuser boolean not null default false ); and a table with jobs: create table job ( id serial primary key, description text ); the jobs can be assigned to users, but only for superusers. other users cannot have jobs assigned. So I have a table whereby I see which job was assigned to which user: create table user_has_job ( user_id integer references "user"(id), job_id integer references job(id), constraint user_has_job_pk PRIMARY KEY (user_id, job_id) ); But I want to create a check constraint that

Use of non-clustered index on guid type column in SQL Server

不想你离开。 提交于 2019-11-30 03:36:19
I would like optimize the performance of a database that my team is using for an application. I have been looking for areas to add foreign keys, and in turn index those columns to improve the performance of joins. However, many of our tables are joined on an id that is a GUID type, generated upon insertion of an item, and the data associated with that item in other tables is generally has column item_id containing the GUID. I have read that adding clustered indexes to GUID type columns is a very bad decision because the index will need to be constantly reconstructed in order to be effective.

Foreign key to composite key

喜夏-厌秋 提交于 2019-11-30 03:33:18
问题 I have a problem i need to reference a single foreign key to a composite key in another table. My database structure is as following: CREATE TABLE available_trip ( trip_code integer not null, date datetime not null, primary key(trip_code, date), FOREIGN KEY (trip_code) REFERENCES trip (trip_code) ); CREATE TABLE booking ( available_trip_code integer not null, customer_code integer not null, date datetime not null, deposit float not null, total_price float not null, has_paid float not null,

How to make ARRAY field with foreign key constraint in SQLAlchemy?

寵の児 提交于 2019-11-30 03:28:06
问题 How to make column with ARRAY(Integer) type, where each integer is primary key from some other table? If it's impossible, how to achieve similar table relationships with other method? 回答1: As of PostgreSQL 9.3, this is not implemented, see http://blog.2ndquadrant.com/postgresql-9-3-development-array-element-foreign-keys/ One should turn array into other table. 来源: https://stackoverflow.com/questions/21932489/how-to-make-array-field-with-foreign-key-constraint-in-sqlalchemy

Rails: violates foreign key constraint

廉价感情. 提交于 2019-11-30 03:01:49
问题 I have three models: Book , genre , BookGenre , and here are relationships: class BookGenre < ActiveRecord::Base belongs_to :book belongs_to :genre end class Book < ActiveRecord::Base has_many :book_genres has_many :genres, through: :book_genres end class Genre < ActiveRecord::Base has_many :book_genres has_many :books, through: :book_genres end And then I use seed file to put data into these tables. But when I want to do rake db:seed again, it showed this error ActiveRecord: