primary-key

Creating tables and problems with primary key in Rails

梦想与她 提交于 2019-11-27 10:07:21
问题 When I try to run the following code in Rails using Mysql2 as database manager: rake db:migrate I obtain the following error: rake aborted! "Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL:" Why do I get this error, if the primary key in a table by default is NOT "null"? Migration code, however : class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string "first_name" t.timestamps end end end 回答1: I had a same problem before, and I solved according to

How can set two primary key fields for my models in Django

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 09:58:12
问题 I have a model like this: class Hop(models.Model): migration = models.ForeignKey('Migration') host = models.ForeignKey(User, related_name='host_set') I want to migration and host both together be the primary key. 回答1: I would implement this slightly differently. I would use a default primary key (auto field), and use the meta class property, unique_together class Hop(models.Model): migration = models.ForeignKey('Migration') host = models.ForeignKey(User, related_name='host_set') class Meta:

Django BigInteger auto-increment field as primary key?

戏子无情 提交于 2019-11-27 09:47:45
问题 I'm currently building a project which involves a lot of collective intelligence. Every user visiting the web site gets created a unique profile and their data is later used to calculate best matches for themselves and other users. By default, Django creates an INT(11) id field to handle models primary keys. I'm concerned with this being overflown very quickly (i.e. ~2.4b devices visiting the page without prior cookie set up). How can I change it to be represented as BIGINT in MySQL and long(

SQL Server “pseudo/synthetic” composite Id(key)

拜拜、爱过 提交于 2019-11-27 09:36:24
Sorry but I don't know how to call in the Title what I need. I want to create an unique key where each two digits of the number identify other table PK. Lets say I have below Pks in this 3 tables: Id Company Id Area Id Role 1 Abc 1 HR 1 Assistant 2 Xyz 2 Financial 2 Manager 3 Qwe 3 Sales 3 VP Now I need to insert values in other table, I know that I may do in 3 columns and create a Composite Key to reach integrity and uniqueness as below: Id_Company Id_Area Id_Role ...Other_Columns..... 1 2 1 1 1 2 2 2 2 3 3 3 But I was thinking in create a single column where each X digites identify each FK.

Does 'Select' always order by primary key?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:03:26
A basic simple question for all of you DBA. When I do a select, is it always guaranteed that my result will be ordered by the primary key, or should I specify it with an 'order by'? I'm using Oracle as my DB. No, if you do not use "order by" you are not guaranteed any ordering whatsoever. In fact, you are not guaranteed that the ordering from one query to the next will be the same. Remember that SQL is dealing with data in a set based fashion. Now, one database implementation or another may happen to provide orderings in a certain way but you should never rely on that. When I do a select, is

What is Hash and Range Primary Key?

余生长醉 提交于 2019-11-27 08:56:35
问题 I am not able to understand what Range primary key is here - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithTables.html#WorkingWithTables.primary.key and how does it work? What do they mean by "unordered hash index on the hash attribute and a sorted range index on the range attribute"? 回答1: " Hash and Range Primary Key " means that a single row in DynamoDB has a unique primary key made up of both the hash and the range key. For example with a hash key of X and

Create association on non-primary key fields with Entity Framework 4.1 Fluent API

别说谁变了你拦得住时间么 提交于 2019-11-27 08:54:35
We are using EF 4.1 and the fluent API to get data from a legacy database (that we are not permitted to change). We are having a problem creating a relationship between two tables where the related columns are not primary and foreign keys. With the classes below, how would we configure the one-to-many relationship between Report and RunStat such that Report.RunStats would return all of the RunStat entities where the ReportCode fields are equal? public class Report { [Key] public int ReportKey { get; set; } public string Name { get; set; } public int ReportCode { get; set; } // Can we associate

adding primary key to sql view

情到浓时终转凉″ 提交于 2019-11-27 08:43:36
Reading that how to do hibernate mapping for table or view without a primary key I am wondering how to add a primary key to my view as it is basically just a stored query...? PS: oracle 10g thx We can add a disabled primary key constraint to a view. That is, the constraint does not fire if an insert or update is run against the view. The database expects integrity to be maintained through constraints on the underlying tables. So the constraint exists solely for the purposes of documentation. SQL> create view emp_view as select * from emp 2 / View created. SQL> alter view emp_view add

SQL Primary Key - is it necessary?

旧巷老猫 提交于 2019-11-27 08:04:43
问题 I have a list of items. Most of these items will not be in stock. The item table has id, name, description. The quantities of items are stored in another table named inventory. The inventory table has item_id and quantity of items that are in stock. Do I need a primary key for the inventory table? If so, should I use a serial or composite key? When is it ok for a table to not have a primary key? Edit: Thank you all for being very informative. I will now always have primary keys except in very

Information schema and Primary Keys

岁酱吖の 提交于 2019-11-27 07:45:43
问题 How do I just print out a 'primary key' for the column with the primary key? I get 'primary key' for all the columns if the table has a primary key, instead of the one column with the primary key and the other columns as blank in keyType. SELECT c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE, c.Column_default, c.character_maximum_length, c.numeric_precision, c.is_nullable, CASE WHEN u.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 'primary key' ELSE '' END AS KeyType FROM INFORMATION_SCHEMA.COLUMNS as c LEFT