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
MetaData reflect is using schema name instead of db name and causing error in SQL Server #4923
Comments
can you try sqlalchemy 1.3.8 please assuming this is 1.3.9, this would indicate a regression caused by #4883 . although this looks like this problem might pre-date that change overall. the "use" you see is that it is attempting to swtich the database from fit_alunos to Guilherme.Santo. However, I don't see why it would do any of that if you used MetaData().refect() and did not pass any kind of schema argument. Can you show the exact code you're running and the full stack trace for the error please. |
do any of the tables in the default schema have foreign key references to tables in the named schemas ? that is, is there cross-schema foreign key reflection going on ? |
Hey, I'm using SQLAlchemy 1.3.10, I can try to downgrade ant test again later. the code I used is exactly the code in the post, but here it is: (I just use getpass to get the password) the complete output for the code is here: I just realized that the problem is not only with the reflection method if try:
I got the same problem with this output: |
All the tables are very simple, I am using just to get used to SQLAlchemy, only one table has an FK but is to an table inside the same schema ("SALAS\Guilherme.santo") |
I just tested with SQLAlchemy 1.3.8 and got the same result. |
it's because of a convention SQLAlchemy has had for many years that assumes there are no dots in a schema name, and instead when it sees a dot in a schema name it assumes that this is actually a I have no solution at the moment, other than patching the code for your specific case which will break everyone elses', and will have to spend a few days trying to think of one. |
here's a patch that I would have to recreate your environent in order to test and I haven't tried this yet. can you try this out? it will apply quoting (brackets) to the names received from the db_name and schema_name functions.
|
that's probably not going to work. |
here's another approach that might work. i think this might also be different in 1.4.
|
Thanks, I imagined that problem could be the schema's name. Unfortunelly I have no control over the schema's names. Thank you for the patch I'll try it as soon as possible and give a feedback. |
the second patch seems to fix it, however the issue also does not occur in master / 1.4 where i had done some refactoring of some of this code, seeing if i can leverage that aspect of it. |
Mike Bayer has proposed a fix for this issue in the master branch: Ensure SQL Server default schema name not interpreted as dot-separated tokens https://gerrit.sqlalchemy.org/1525 |
Mike Bayer has proposed a fix for this issue in the rel_1_3 branch: Ensure SQL Server default schema name not interpreted as dot-separated tokens https://gerrit.sqlalchemy.org/1526 |
I would not be surprised if sqlalchemy has more issues with this naming convention, however the immediate issue should be fixed by that patch going through. if you can try it out and then do a little more with Core or ORM maybe we can find some more issues to fix. |
Hey, I've tested some commands with the core language and the ORM and, until now, it seems to be working just fine, thanks again for the patch. if I found another bug related to the schema name I'll post it here. |
OK we'll commit that and see how it goes. when you use these schema names, you need to put brackets around them, the mssql dialect in SQLAlchemy will then know to not split out on the dot to get the "owner" / "database" from it. it's not ideal but that's where we're at. see https://docs.sqlalchemy.org/en/13/dialects/mssql.html#multipart-schema-names |
…d tokens Fixed an issue in the :meth:`.Engine.table_names` method where it would feed the dialect's default schema name back into the dialect level table function, which in the case of SQL Server would interpret it as a dot-tokenized schema name as viewed by the mssql dialect, which would cause the method to fail in the case where the database username actually had a dot inside of it. In 1.3, this method is still used by the :meth:`.MetaData.reflect` function so is a prominent codepath. In 1.4, which is the current master development branch, this issue doesn't exist, both because :meth:`.MetaData.reflect` isn't using this method nor does the method pass the default schema name explicitly. The fix nonetheless guards against the default server name value returned by the dialect from being interpreted as dot-tokenized name under any circumstances by wrapping it in quoted_name(). Fixes: #4923 Change-Id: I821bd38ed89b767eaca0bdffee7f8ba3baf82560 (cherry picked from commit f50c6a04067acf2cd2fc5e42d5acaa9206d9a078)
I'm currently working in a SQL SERVER database, over a network, named "fit_alunos" that contains a lot of schemas with the pattern "SALAS{first name}.{last name}"
I can create the engine and connect to the database without problem:
then if I inspect the engine, everything seems ok:
but if I try to create a
MetaData
object and reflect the engine:I got this
OperationalError
:I guess that SQLAlchemy is interpreting that the database is "SALAS\Guilherme" and the schema is "Santo", instead of the the database "fit_alunos" and the schema "SALAS\Guilherme.Santo".
So, I ran the reflect method wit an engine with echo=True and find that it gets the db name using a SQL function:
It seems that
SELECT db_name()
is returning the schema name instead of the db name.But then, I tested the return value from the SQL functions that get the DB name and schema name, and it seems to be right
Any idea of what can be happening?
The text was updated successfully, but these errors were encountered: