On 01/18/2017 05:20 AM, Chris Withers wrote:
On 17/01/2017 15:07, mike bayer wrote:

Because there's no data in a brand new table, the server default isn't
needed to create the not-null column.

No needed, but it is created, isn't that the point of
autoincrement=True?

for postgresql, autoincrement=True means that if the column is marked
primary_key=True it will use the datatype SERIAL for that column, which
in PG does mean the sequence is generated and added as the server side
default.

Would you expect a SERIAL on Postgres to end up being an inteeger with a
default of a sequence when viewed in psql?

What happens with autoincrement on a non-primary-key column? My
experiences suggests it does nothing...

    http://stackoverflow.com/a/19262262/216229 implies the same works
    for Alembic, but it didn't work at all for me. So I was
wondering if
    that answer was wrong, or if I was doing something wrong.

Still wondering if this answer is wrong or if I'm doing something
wrong...

Well in fact now that i've read the original SO question, the answer you refer to is completely wrong. The correct answer to the question as asked is:

op.execute("CREATE SEQUENCE group_id_seq")
op.create_table(
    'groups',
    Column('id', Integer, Sequence('group_id_seq'), primary_key=True),
    Column('name', Unicode(50)),
    Column('description', Unicode(250)),
)

the poster also seems to have figured out this syntax:

op.execute(CreateSequence(Sequence(name)))

which is fine, just more work (because Sequence is pretty much only a Postgresql / Oracle thing and you say CREATE SEQUENCE on either, will fail on all other DBs except firebird)


this however has nothing to do with your issue of the NOT NULL constraint failing, because in your case, you have data in your table and you need a server side default. The SO question as asked does not create a server side default.



that SO answer is showing op.create_table().   your example was just for
op.add_column().  super different.

I have to admit that, at the column level, that's surprising to me.
Where can I see the differences between a column created as part of
create_table() verus as part of an add_column()?


SQL wise the syntaxes are:

https://www.postgresql.org/docs/9.1/static/sql-createtable.html

https://www.postgresql.org/docs/9.1/static/sql-altertable.html

Alembic op.add_column() will emit the SERIAL if you send a Column with primary_key=True and Integer datatype on a Postgresql backend. I just added this to confirm:

+    def test_col_w_pk_is_serial(self):
+        context = op_fixture("postgresql")
+        op.add_column("some_table", Column('q', Integer, primary_key=True))
+        context.assert_(
+            'ALTER TABLE some_table ADD COLUMN q SERIAL NOT NULL'
+        )


SERIAL would be all you need here.



cheers,

Chris


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to