Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i add auto_increment column to work with mysql and postgres databases? #169

Open
0xff00ff opened this issue May 26, 2016 · 5 comments

Comments

@0xff00ff
Copy link

I want to create migration for mysql and postgres databases, but i need an autoincrement column. For mysql it`s simple, auto_increment option works well. but how about postgres?

@ruckus
Copy link
Owner

ruckus commented May 26, 2016

The Postgres analog to MySQL auto_increment is serial. So the actual SQL would be:

CREATE TABLE animals (id serial, name text)

To achieve this:

$t = $this->create_table('animals');
// will automatically give you an `id` column of type `serial`

If you wanted a different name than id

// specify id=false so the default primary key is ignored
$table = $bm->create_table('animals', array('id' => false));
$table->column('user_id', 'integer', array('primary_key' => true, 'auto_increment' => true));
$sql = $table->finish();

@0xff00ff
Copy link
Author

@ruckus thanks for answer, but just option 'primary_key' is not works. but if i set 'primary_key' as column type (not integer) it works perfect.

@jaffarhussain1011
Copy link

@ruckus i am able to create primary key with different column name but I want to specify my own keyname/constraint name so that sql for that should be like below
CONSTRAINT my_custom_key_anme PRIMARY KEY (user_id)
Is there any option to provide that in following query:
$table->column('user_id', 'integer', array('primary_key' => true, 'auto_increment' => true));

Thanks

@ruckus
Copy link
Owner

ruckus commented Feb 2, 2017

Hi @jaffarhussain1011 - you cannot specify a constraint in the same column() method call. You'll need to finish creating the table and then run ad-hoc SQL to add that constraint, like:

// ... column calls
$table->finish();
$this->execute("ALTER TABLE foobar ADD CONSTRAINT ...");

@jaffarhussain1011
Copy link

Ok, Thanks for prompt reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants