foreign-keys

Foreign key definition in sqlite

我与影子孤独终老i 提交于 2019-11-30 21:33:29
问题 Cant add foreign key constraint in sqlite ........... 回答1: As of SQLite 3.6.19, SQLite supports foreign keys. You need to enable them via: sqlite> PRAGMA foreign_keys = ON; They are turned off by default for backwards compatibility. See the documentation for more details. 回答2: sqlite does not enforce foreign key constraints. 回答3: in sqlite 3 : examples : create table student (_id integer autoincrement primary key ,master_id integer); create table master (_id integer autoincrement primary key

MySQL - Unique foreign key

可紊 提交于 2019-11-30 20:45:48
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? Every column with an key (primary, foreign) needs an index. Same with column being unique. You probably created two indexes (one when creating FK and one on Unique constraint). If this is the case just drop one of those indexes. It is overhead

NHibernate sets Foreign Key in secondary update rather than on initial insert violating Not-Null Constrain on key column

让人想犯罪 __ 提交于 2019-11-30 20:39:09
I am having a problem with what should be a fairly simple (I would think) NHibernate use case. I have a classic Parent and a Child entity like so: public class Parent { public virtual int ParentId { get; set; } public virtual string Name { get; set; } public virtual IList<Child> Children { get; set; } } public class Child { public virtual int ChildId { get; set; } public virtual Parent Parent { get; set; } public virtual string Name { get; set; } } And mappings as follows: public class ParentMap : ClassMap<Parent> { public ParentMap() { Id(x => x.ParentId).GeneratedBy.Native(); Map(x => x.Name

Hibernate: Foreign key has the wrong number of columns

喜夏-厌秋 提交于 2019-11-30 20:37:25
I have defined a many-to-many relationship between my two entity classes User and Permission. User has a primary key composite of username and countyId, and my Permission table has a regular integer Id. The table UserPermission has the three foreign keys as its primary key: username, countyId and permissionId. Since this is a legacy database, I won't have the opportunity to do the Right Thing(™) and make an integer primary key on User. I've defined the many-to-many relationship like this in User.class: @ManyToMany(targetEntity=Permission.class, cascade={ CascadeType.PERSIST, CascadeType.MERGE

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

こ雲淡風輕ζ 提交于 2019-11-30 20:10:26
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__ = 'asset' user = db.Column('usuario', db.Integer, primary_key=True) profile = db.Column('perfil', db.Integer,

Django File Upload and Rename

。_饼干妹妹 提交于 2019-11-30 20:09:36
User is uploading a .c file of a particular question. I want the file to be renamed as 'userid_questionid.c' My models.py is : from django.db import models class users(models.Model): username = models.CharField(max_length=20) password = models.CharField(max_length=20) score=models.IntegerField(max_length=3) def __unicode__(self): return self.username class questions(models.Model): question = models.TextField(max_length=2000) qid=models.IntegerField(max_length=2) def __unicode__(self): return self.qid def content_file_name(instance, filename): return '/'.join(['uploads', instance.questid.qid,

Why aren't my sqlite3 foreign keys working?

我的未来我决定 提交于 2019-11-30 20:08:30
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); ... create table t2 (i, a, foreign key (i) references t1(i)); ... commit; ... """) <sqlite3.Cursor object at

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

隐身守侯 提交于 2019-11-30 19:16: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? 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

Foreign key to composite key

我怕爱的太早我们不能终老 提交于 2019-11-30 19:09:47
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, description_en nvarchar(12) null, finance_type_code nvarchar(12) not null, primary key(available_trip

Rails: violates foreign key constraint

旧巷老猫 提交于 2019-11-30 19:07:37
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::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "books" violates foreign key constraint