Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Generated SQL throws error only on specific tables

I am accessing MS SQLServer through Laravel, using Eloquent ORM or Query Builder. This works fine on all tables, but one particular table throws this error:

production.ERROR: exception 'Illuminate\Database\QueryException' 
with message 'SQLSTATE [HY000]: General error: 4004 General SQL Server error: 
Check messages from the SQL Server [4004] (severity 16) [(null)] 
(SQL: select * from [tblLeistung])

I executed the given SQL query in MS SQLServer Management Studio and it works fine. I also tried adding the DB name and schema name in the ORM protected $table = 'DBName.dbo.tblLeistung'; which properly generates (SQL: select * from [DBName].[dbo].[tblLeistung]) but throws the same error, too.

The issue seems to be with the schema, since it is a rather large table (23 columns, 2160 records). Dropping it and recreating it with the same schema still causes the error. Dropping it and creating a simple table with the same name solves the error (however I need to make it work with the given schema).
Anyone has an idea whether there are specific table designs that are unsupported by a remote access of a SQLServer through Laravel?
Are there other (simple) ways to test, whether the problem is with Laravel or with the Server?

I've been at this for some time now and appreciate any help. Thanks in advance!

A generated CREATE TABLE statement of the problematic table looks like this:

CREATE TABLE [dbo].[tblLeistung](
    [LeistungID] [int] IDENTITY(1,1) NOT NULL,
    [LeistungskatalogID] [int] NULL,
    [SchlagID] [int] NOT NULL,
    [StatusInternID] [int] NULL,
    [StatusExternID] [int] NULL,
    [VertragID] [int] NULL,
    [Menge] [float] NULL DEFAULT ((0)),
    [SperreLeistung] [bit] NULL DEFAULT ((0)),
    [SperreAusfuehrung] [bit] NULL DEFAULT ((0)),
    [RechnungEinkID] [int] NULL,
    [RechnungVerkID] [int] NULL,
    [LeistungsunterkatID] [int] NULL,
    [LeistungsBeschreibung] [nvarchar](max) NULL,
    [InterneNotiz] [nvarchar](max) NULL,
    [AbrechungsdatenVorhanden] [bit] NULL DEFAULT ((0)),
    [AuftragID] [smallint] NULL,
    [UeberleistungID] [int] NULL,
    [AuftraggeberID] [int] NULL,
    [SSMA_TimeStamp] [timestamp] NOT NULL,
    [DatumFrist] [datetime] NULL,
    [DatumGeplant] [datetime] NULL,
    [DatumAusgefuehrt] [datetime] NULL,
    [DatumAuftragfreigabe] [datetime] NULL,
 CONSTRAINT [tblLeistung$PrimaryKey] PRIMARY KEY CLUSTERED 
(
    [LeistungID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

ALTER TABLE [dbo].[tblLeistung]  WITH NOCHECK ADD  CONSTRAINT [tblLeistung$tbRechnungVerktblLeistung] FOREIGN KEY([RechnungVerkID])
REFERENCES [dbo].[tblRechnungVerk] ([RechnungVerkID])
ON UPDATE CASCADE

ALTER TABLE [dbo].[tblLeistung] CHECK CONSTRAINT [tblLeistung$tbRechnungVerktblLeistung]

edit:

  • removing the FOREIGN KEY didn't solve it
  • the same error occurs if I access a table that doesn't exist. However after creating the same table on several DB instances with different configurations, I am pretty confident it exists.
like image 988
FrankSchrank Avatar asked Feb 19 '26 03:02

FrankSchrank


1 Answers

Laravel uses db-library (if it's available) to connect to Sql Server which cannot receive unicode data from MSSQL. (1,2)

If you don't have to save unicode data in 'LeistungsBeschreibung', change the column datatype to varchar.

You may want to checkout the accepted answer to a similar question: MSSQL Query issue in PHP and querying text data

Links

  • http://msdn.microsoft.com/en-us/library/aa937590(v=sql.80).aspx (1)
  • https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Connectors/SqlServerConnector.php#L47 (2)
like image 167
jithujose Avatar answered Feb 20 '26 17:02

jithujose