auto-increment

postgresql nextval generating existing values

我们两清 提交于 2019-12-05 06:31:53
I had to migrate from a mySql based ruby on rails app to using postgresql. No problems but one so far, and I don't know how to solve it. The migration of data brought ids along with it, and postgresql is now having problems with existing ids: it's not clear to me where it gets the value that it uses to determine the base for nextval: it certainly isn't the highest value in the column, although you might think that would be a good idea. In any case, it's now colliding with existing id values. id column, created from a standard RoR migration is defined as not null default nextval('geopoints_id

Copy autoincrement value to another column on insert?

时间秒杀一切 提交于 2019-12-05 05:26:04
Basically I have a table that versions products, so it has two columns of interest, id | product_id id is an autoincrement column, product_id is just an int When a product is first created the product_id comes from the id , When the product is edited we duplicate the row, so the product_id is the same, but the id is different. so when we first create the product we do two queries, insert, then update table whatever set product_id = id where id = {the insert id} This works, but I am wondering if there is a way to do it in one query? Note we only have access to insert, update, delete. no

In Rails how would one generate a unique serial number for a new instance of a model?

怎甘沉沦 提交于 2019-12-05 04:29:06
问题 In Rails, I'm looking for a way to generate an auto-incrementing serial number for internal record keeping for new instances of a model. I would like to avoid creating database specific code and rather have a solution that will work regardless of the database. My current idea is to wait until the model is saved and then grab the ID of the saved model and use that as a suffix of the serial number, but then I would have to save each record twice on creation. Any ideas? Thanks for looking! 回答1:

How can i change _id field in MongoDB Collection to User_id?

别说谁变了你拦得住时间么 提交于 2019-12-05 01:49:00
I am new user for MongoDB Database. In MongoDb whatever insert into some collection defaultly one field is added that is _id field. For Example: db.users.insert({"User_id":"1","User_Name":"xxx","Address":"yyyy"}) db.users.find() It shows { "_id" : ObjectId("528475fc326b403f580d2eba"), "User_id" : "1", "User_Name" : "xxx",Address" : "yyyy" } I don't need _id field and i want to replace that _id field to User_id with auto increment values. Is it possible. Please help me. Thanks in advance. _id field is really special in mongodb. This is your primary key there and there is no way you can have a

Change Autoincrement values in migration (PostgreSQL and SQLite3)

青春壹個敷衍的年華 提交于 2019-12-05 01:24:17
问题 I have a project hosted on Heroku and would like to change a table's autoincrement start value. I'm using SQLite3 locally and Heroku uses PostgreSQL This is what I have in the migration: class CreateMytable < ActiveRecord::Migration def self.up create_table :mytable do |t| t.text :mytext end case ActiveRecord::Base.connection.adapter_name when 'PostgreSQL' execute 'ALTER SEQUENCE mytable_id_seq RESTART WITH 1000;' when 'SQLite' execute 'update sqlite_sequence set seq = 1000 where name =

How can I get the auto incrementing field name or the primary key fieldname from a mysql table?

半世苍凉 提交于 2019-12-04 22:16:39
问题 In PHP, how do I get the field name of the field that's been set as to auto increment when a new rec is added to it? In most cases, it's the same as the PRIMARY_KEY of the table but not necessarily always. So this question has 2 parts with the second one branching into a 3rd part. 1- How to get the name of the auto-incrementing field name... 2- How to get the name of the primary_key field name... 2.1 How to get the primary_key(s) info when a table uses more than one field as its primary key..

Large Auto Increment IDs

强颜欢笑 提交于 2019-12-04 22:00:48
I'm having a strange issue with some auto-inc IDs on a table, where instead of going up by 1 each time, it seems to be going up by 10 each time. I'm using the ClearDB MySQL Addon for Heroku PHP 5.5.15 Apache 2.4.10 Laravel dev branch (4.something) I've created the database via artisan's migrate functionality, My migration for the database table is <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCheckinTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('checkins', function

How to find next free unique 4-digit number

泪湿孤枕 提交于 2019-12-04 21:51:31
In my db application I have a requirement for a unique, 4-digit number field for each customer. Up until 9999 I can just use autoincrements, but after that I will have to reuse numbers of customers that have been deleted (there won't be more than 5000 customers at a given time but there may be more than 9999 customers over the lifetime of the system). Question 1: Is there a (My)SQL statement to find the next reusable free number? Question 2: If I get the number, assign it to a new customer and save the customer all in one transaction, similar transactions taking place at the same time will be

Pylons, SQlite and autoincrementing fields

早过忘川 提交于 2019-12-04 21:09:42
问题 Hey! Just started working with Pylons in conjunction with SQLAlchemy, and my model looks something like this: from sqlalchemy import Column from sqlalchemy.types import Integer, String from helloworld.model.meta import Base class Person(Base): __tablename__ = "person" id = Column(Integer, primary_key=True) name = Column(String(100)) email = Column(String(100)) def __init__(self, name='', email=''): self.name = name self.email = email def __repr__(self): return "<Person('%s')" % self.name To

How to enable auto-increment with Sql compact & C#

若如初见. 提交于 2019-12-04 21:08:46
I'm using C# 2010 Express. I have created a SQL Server CE database file ( .sdf ) then created Information table and ID, Name, City columns with Database Explorer. I want to save names and city user will write to the form. The auto-increment option is disabled in Database explorer and Properties windows. When I'm saving something to that database i must change the ID manually, how can I make ID column auto-increment enabled? There is a simple way for SQL CE: ALTER TABLE TableName ALTER COLUMN ColumnName IDENTITY (/*seed*/1, /*increment*/1) 来源: https://stackoverflow.com/questions/5052922/how-to